
C    Copyright 2011 Prof K.Sridharan
C    This file is part of SAP2
C
C    SAP2 is free software: you can redistribute it and/or modify
C    it under the terms of the GNU General Public License as published by
C    the Free Software Foundation, either version 3 of the License, or
c    (at your option) any later version.
C
C    SAP2 is distributed in the hope that it will be useful,
C    but WITHOUT ANY WARRANTY; without even the implied warranty of
C    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
C    GNU General Public License for more details.
C
C   You should have received a copy of the GNU General Public License
C    along with SAP2.  If not, see <http://www.gnu.org/licenses/>.

c       editing for printing cmax in air vessel for volume>1000m3.

       CHARACTER * 80 PROJECT,CASE,CTDRG(10),PILOT(10,100)
       CHARACTER * 10 CODNRR(10),CODNRB(10),CODEAC,ACNRV,
     * CODECS,KSVST(10,100),CODSIM,SIML,CODIV,CODBIV
       CHARACTER * 3 TYPE(50),CODEPR,CODPMP(10),CODBST(5)
       CHARACTER * 60 DEVICE(11)
       INTEGER TOST1,TOST2,TOST3,TOST4,TAVS,TRPM	

       DIMENSION IND1(50),IND2(50),PDC(50),PDIA(50),PLEN(50),WV(50),
     * NUSP(50),NDSP(50),IUSP(50,5),IDSP(50,5),IRES(50),ISOU(50),
     * ICDS(50),IOBS(50),IPMP(50),IBST(50),HGL(50),RESWL(50),
     * RESDC(50),SOUWL(5),SOUDC(5),HGL1CD(5),HGL2CD(5),QOCDS(5),
     * HGL1OB(5),HGL2OB(5),QOOBS(5),NPUMPS(10),PUMPDC(10),QRPMP(10),
     * PUMPH(10),HRPMP(10),IPMPSP(10),GD2PMP(10),GD2PM(10),SUMPWL(10),
     * WHP(10,89),WBP(10,89),NBOOST(5),BSTDC(5),QRBST(5),BSTH(5),
     * HRBST(5),IBSTSP(5),GD2BST(5),GD2BM(5),HGLSUC(5),KODPHV(10),
     * KODBSV(5),WHB(5,89),WBB(5,89),NPDSPM(10),NPDSBS(5),
     * NPUSCD(5),NPUSOB(5),NPUSRS(50),NPDSSO(5),CHST(50),CHSV(10),
     * NALIGN(50),CHAIN(50,400),GL(50,400),NPPRNT(500),IPRNT(500),
     * CHPRNT(500),ITRIP(10),ITRIPB(5),EFFP(10),EFFB(5),
     * HLCDS(5),HLOBS(5),NNPRNT(500),NPCS(5),CHCS(5),PLCS(5),
     * HGL1(50),HGL2(50),TCLOSE(10),TRAPID(10),TSLOW(10),DLYPH(10),
     * TCLOSB(5),TRAPDB(5),TSLOWB(5),DLYBS(5),IPRED(5),ISUCC(5),
     * CPCJ(5),VCJGN(5),CMDJ(5),VDJGN(5),QSVD(10),VSVMXD(10),
     * TVSVD(10),NPPATH(7),IPPATH(7,50),KODEDV(10),NSN(10),CHDEV(10)

       DIMENSION CHOST(40),GLOST(40),STGOST(40),DOST(40),
     * YSTOST(40),DCOST(40),DLYRV(40),DBYNRV(40),CHZVV(40),CHDPCV(40),
     * CHINRV(40),SZAIRV(40),CHAIRV(40),AVINV(40),CHSS(40),
     * SSDIA(40),SSINV(40),SSTOP(40),CHNODE(500),
     * DRGOST(10),TDRG(10),TVOD(10),HMIND(600),HMAXD(600),DBYCV(10),	
     * TSVST(5,100),TSVSTD(5,100),SZACV(40),CHACV(40),ISVST(5),
     * ACVINV(40),VCOSTD(40),NNSV(5),SIZSV(5),SVINV(5),PLOWSV(5),
     * PHISV(5),CTIMSV(5),NPSV(5),NPOST(40),NPINRV(40),NPZVV(40),
     * NPDPCV(40),NPAIRV(40),NPACV(40),NPSS(40)

       DIMENSION B(50),HF(50),VI(50),R(50),NGAP(50),NEND(50),ICV(10),
     * TC(10),TC1(10),TC2(10),DLY(10),ICVB(5),TCB(5),TC1B(5),TC2B(5),
     * TA(10),TAB(5),HU(600),HD(600),VU(600),VD(600),HNU(600),DL(50),
     * HND(600),VNU(600),VND(600),HUMIN(600),HDMIN(600),HUMAX(600),
     * HDMAX(600),TUMIN(600),TDMIN(600),TUMAX(600),TDMAX(600),
     * VUMIN(600),VDMIN(600),VUMAX(600),VDMAX(600),HGLR(50),
     * TVUMN(600),TVDMN(600),TVUMX(600),TVDMX(600),HSOU(5),
     * IAV(10),ZAV(10),AVC(10),IACV(10),ZACV(10),ACVC(10),TAU(42),
     * INRV(10),DLYNRV(10),DHBNRV(10),TVONRV(10),OG(21),
     * TAUZV(21),IZV(10),TAZV(10),AR(50),HRATB(5),CIB(5),
     * HRAT(10),PUMPK(10),CI(5),DHOB(5),DHO(10),BSTK(5),VPUIB(5),
     * DLYB(5),HPU(10),HPUO(10),HPUB(5),HPUOB(5),ALB(5),ALOB(5),
     * BETB(5),BETOB(5),TVOB(5),KIB(5),VPUB(5),VPUOB(5),VPUI(10),
     * VPU(10),VPUO(10),AL(10),ALO(10),BET(10),BETO(10),TVO(10),KI(10),
     * ALMIN(10),TALMIN(10),ALMAX(10),TALMAX(10),BETMIN(10),TBETMN(10),
     * BETMAX(10),TBETMX(10),HPUMIN(10),THPUMN(10),HPUMAX(10),
     * THPUMX(10),VPUMIN(10),TVPUMN(10),VPUMAX(10),TVPUMX(10),ZPU(10),
     * ALMINB(10),TALMNB(10),ALMAXB(10),TALMXB(10),BETMNB(10),
     * TBTMNB(10),BETMXB(10),TBTMXB(10),HPUMNB(10),THPMNB(10),
     * HPUMXB(10),THPMXB(10),VPUMNB(10),TVPMNB(10),VPUMXB(10),
     * TVPMXB(10),IOST(10),OSTC(10),HTOST(10),HLOST(10),OSTK(10),
     * HOST(10),VOST(10),HOSTMN(10),HOSTMX(10),TOSTMN(10),TOSTMX(10),	
     * TDOST(10),ISV(10),ZSV(10),CSV(10),OSTBED(10),HOSTO(10),
     * VOSTMX(10),TCSV(10),TSV(10),HMNSV(10),HMXSV(10),KSV(10),OSV(10),
     * AIRC(10),VSVMN(10),TVSVMN(10),VSVMX(10),TVSVMX(10),VSV(10),
     * QSV(10),ISS(5),SSC(5),SSCK(5),HTSS(5),TVOSTM(10),KDNRR(10),
     * HSS(5),ZSS(5),HSSMAX(5),HSSMIN(5),TSSMAX(5),TSSMIN(5),HNSS(5),
     * OGZVV(21),IPILOT(10,100),KDNRRB(5),ICAV(5),VCAV( 5),ZCS(5),
     * TAUZVV(21),OGDP(21),TAUDP(21),DHZ(10),DHB(10),DHZB(10),ICS(5),
     * TOSTS(10),TOSTE(10),FKNRR(10),FKNRRB(5),VOLAIR(10),QAIRMX(10),
     * NSTART(50),QAIR(10),TVODB(5),VCAVMX(5),TCAVMX(5),DVCVMX(5),
     * TDVCMX(5),DVCVMN(5),TDVCMN(5),KCOLSP(5),IOROST(10),IORZV(10),
     * IORNRV(10),IORAV(40),IORACV(40),IORSS(10),IORSV(10),IORCS(10)

	OPEN(UNIT=1,FILE='c:\iisc\SAP2.DAT')
        OPEN(UNIT=2,FILE='c:\iisc\ALIGN2.DAT')
        OPEN(UNIT=3,FILE='c:\iisc\WHWB2.DAT')	
	OPEN(UNIT=4,FILE='c:\iisc\KS.S11')
	OPEN(UNIT=5,FILE='c:\iisc\SAP2.RES')
	OPEN(UNIT=6,FILE='c:\iisc\F6.RES')
	OPEN(UNIT=8,FILE='c:\iisc\TABLE.RES')

	OPEN(UNIT=7,FILE='c:\iisc\RATE.DAT')
	OPEN(UNIT=11,FILE='c:\iisc\VOST1.DAT')
	OPEN(UNIT=21,FILE='c:\iisc\OST1.DAT')
	OPEN(UNIT=12,FILE='c:\iisc\VOST2.DAT')
	OPEN(UNIT=22,FILE='c:\iisc\OST2.DAT')
	OPEN(UNIT=13,FILE='c:\iisc\VOST3.DAT')
	OPEN(UNIT=23,FILE='c:\iisc\OST3.DAT')
	OPEN(UNIT=14,FILE='c:\iisc\VOST4.DAT')
	OPEN(UNIT=24,FILE='c:\iisc\OST4.DAT')
	OPEN(UNIT=10,FILE='c:\iisc\SPEED.DAT')
	OPEN(UNIT=20,FILE='c:\iisc\RPM.DAT')
	OPEN(UNIT=15,FILE='c:\iisc\VAV.DAT')
	OPEN(UNIT=25,FILE='c:\iisc\AVS.DAT')
	OPEN(UNIT=26,FILE='c:\iisc\PLOT1.DAT')
	OPEN(UNIT=27,FILE='c:\iisc\PLOT2.DAT')
	OPEN(UNIT=28,FILE='c:\iisc\PLOT3.DAT')
	OPEN(UNIT=29,FILE='c:\iisc\PLOT4.DAT')
	OPEN(UNIT=30,FILE='c:\iisc\PLOT5.DAT')
	OPEN(UNIT=31,FILE='c:\iisc\PLOT6.DAT')

	CLOSE(UNIT=4,STATUS='DELETE')
	CLOSE(UNIT=5,STATUS='DELETE')
	CLOSE(UNIT=6,STATUS='DELETE')
	CLOSE(UNIT=8,STATUS='DELETE')
	CLOSE(UNIT=7,STATUS='DELETE')
	CLOSE(UNIT=11,STATUS='DELETE')
	CLOSE(UNIT=21,STATUS='DELETE')
	CLOSE(UNIT=12,STATUS='DELETE')
	CLOSE(UNIT=22,STATUS='DELETE')
	CLOSE(UNIT=13,STATUS='DELETE')
	CLOSE(UNIT=23,STATUS='DELETE')
	CLOSE(UNIT=14,STATUS='DELETE')
	CLOSE(UNIT=24,STATUS='DELETE')
	CLOSE(UNIT=10,STATUS='DELETE')
	CLOSE(UNIT=20,STATUS='DELETE')
	CLOSE(UNIT=15,STATUS='DELETE')
	CLOSE(UNIT=25,STATUS='DELETE')
	CLOSE(UNIT=26,STATUS='DELETE')
	CLOSE(UNIT=27,STATUS='DELETE')
	CLOSE(UNIT=28,STATUS='DELETE')
	CLOSE(UNIT=29,STATUS='DELETE')
	CLOSE(UNIT=30,STATUS='DELETE')
	CLOSE(UNIT=31,STATUS='DELETE')

	OPEN(UNIT=4,FILE='c:\iisc\KS.S11')
	OPEN(UNIT=5,FILE='c:\iisc\SAP2.RES')
	OPEN(UNIT=6,FILE='c:\iisc\F6.RES')
	OPEN(UNIT=8,FILE='c:\iisc\TABLE.RES')
	OPEN(UNIT=7,FILE='c:\iisc\RATE.DAT')
	OPEN(UNIT=11,FILE='c:\iisc\VOST1.DAT')
	OPEN(UNIT=21,FILE='c:\iisc\OST1.DAT')
	OPEN(UNIT=12,FILE='c:\iisc\VOST2.DAT')
	OPEN(UNIT=22,FILE='c:\iisc\OST2.DAT')
	OPEN(UNIT=13,FILE='c:\iisc\VOST3.DAT')
	OPEN(UNIT=23,FILE='c:\iisc\OST3.DAT')
	OPEN(UNIT=14,FILE='c:\iisc\VOST4.DAT')
	OPEN(UNIT=24,FILE='c:\iisc\OST4.DAT')
	OPEN(UNIT=10,FILE='c:\iisc\SPEED.DAT')
	OPEN(UNIT=20,FILE='c:\iisc\RPM.DAT')
	OPEN(UNIT=15,FILE='c:\iisc\VAV.DAT')
	OPEN(UNIT=25,FILE='c:\iisc\AVS.DAT')
	OPEN(UNIT=26,FILE='c:\iisc\PLOT1.DAT')
	OPEN(UNIT=27,FILE='c:\iisc\PLOT2.DAT')
	OPEN(UNIT=28,FILE='c:\iisc\PLOT3.DAT')
	OPEN(UNIT=29,FILE='c:\iisc\PLOT4.DAT')
	OPEN(UNIT=30,FILE='c:\iisc\PLOT5.DAT')
	OPEN(UNIT=31,FILE='c:\iisc\PLOT6.DAT')


C READ THE INPUT DATA - FROM VB DUMP

	READ(1,*)PROJECT
	READ(1,*)CASE
	READ(1,*)SIML
	READ(1,*)NPMAX,NNMAX
	DO I= 1,NPMAX
	 READ(1,*)ID,IND1(I),IND2(I),PDC(I),PDIA(I),PLEN(I),
     *   WV(I),CHST(I)
	END DO
	DO I = 1,NNMAX
	 READ(1,*)NNO,TYPE(I),NUSP(I),NDSP(I)	
  	 DO J= 1,NUSP(I)
   	  READ(1,*)IUSP(I, J)
  	 END DO
  	 DO J = 1,NDSP(I)
   	  READ(1,*)IDSP(I,J)
  	 END DO
	END DO

	DO I = 1,NNMAX
 	 READ(1,*)IRES(I),ISOU(I),ICDS(I),IOBS(I),IPMP(I),
     *            IBST(I),HGL(I)
	END DO
	READ(1,*)NORD,NCJN,NDJN,NRES,NSOU,NCDS,NOBS,NPMP,NBST
	DO JJ = 1,NRES
 	 READ(1,*)RESWL(JJ),RESDC(JJ),NPUSRS(JJ)
	END DO
	KODEDS=0
	IF(NRES.EQ.1)THEN
	  READ(1,*)KODEDS,DLYDSD,TOPDSD
	END IF
	DO JJ = 1,NSOU
	 READ(1,*)SOUWL(JJ),SOUDC(JJ),NPDSSO(JJ)
	END DO
	DO JJ = 1,NCDS
	 READ(1,*)HGL1CD(JJ),HGL2CD(JJ),QOCDS(JJ),NPUSCD(JJ)
	END DO
	DO JJ = 1,NOBS
	 READ(1,*)HGL1OB(JJ),HGL2OB(JJ),QOOBS(JJ),NPUSOB(JJ)
	END DO
	DO JJ = 1,NPMP
	READ(1,*)NPUMPS(JJ),PUMPDC(JJ),QRPMP(JJ),PUMPH(JJ),HRPMP(JJ),
     *  EFFP(JJ),IPMPSP(JJ),CODPMP(JJ),GD2PMP(JJ),GD2PM(JJ),
     *  SUMPWL(JJ),KODPHV(JJ),CODNRR(JJ),FKNRR(JJ),NPDSPM(JJ),ITRIP(JJ)
	END DO
	DO JJ = 1,NBST
	READ(1,*)NBOOST(JJ),BSTDC(JJ),QRBST(JJ),BSTH(JJ),HRBST(JJ),
     *  EFFB(JJ),IBSTSP(JJ),CODBST(JJ),GD2BST(JJ),GD2BM(JJ),HGLSUC(JJ),
     *  KODBSV(JJ),CODNRB(JJ),FKNRRB(JJ),NPDSBS(JJ),ITRIPB(JJ)
	END DO
 
	DO JJ = 1,NPMP
	  DO KK = 1,89
          READ(3,*)WHP(JJ,KK),WBP(JJ,KK)
	  END DO
	END DO
	DO JJ = 1,NBST
	  DO KK = 1,89
	  READ(3,*)WHB(JJ,KK),WBB(JJ,KK)
	  END DO
	END DO

	DO JJ = 1,NPMP
 	  IF(KODPHV(JJ).EQ.1)THEN
          READ(1,*)DLYPH(JJ)
 	  ELSEIF(KODPHV(JJ).EQ.2)THEN
          READ(1,*)TCLOSE(JJ),DLYPH(JJ)
 	  ELSEIF(KODPHV(JJ).EQ.3)THEN
          READ(1,*)TRAPID(JJ),TSLOW(JJ),DLYPH(JJ)
 	  ELSEIF(KODPHV(JJ).EQ.4)THEN
          READ(1,*)TCLOSE(JJ)
 	  ELSEIF(KODPHV(JJ).EQ.5)THEN
          READ(1,*)TRAPID(JJ),TSLOW(JJ)
 	  END IF
	END DO
	DO JJ = 1,NBST
 	  IF(KODBSV(JJ).EQ.1)THEN
          READ(1,*)DLYBS(JJ)
 	  ELSEIF(KODBSV(JJ).EQ.2)THEN
          READ(1,*)TCLOSB(JJ),DLYBS(JJ)
 	  ELSEIF(KODBSV(JJ).EQ.3)THEN
          READ(1,*)TRAPDB(JJ),TSLOWB(JJ),DLYBS(JJ)
 	  ELSEIF(KODBSV(JJ).EQ.4)THEN
          READ(1,*)TCLOSB(JJ)
 	  ELSEIF(KODBSV(JJ).EQ.5)THEN
          READ(1,*)TRAPDB(JJ),TSLOWB(JJ)
 	  END IF
	END DO
	DO JJ = 1,NPMAX
	  READ(2,*)NALIGN(JJ)
	  DO KK = 1,NALIGN(JJ)
          READ(2,*)CHAIN(JJ,KK),GL(JJ,KK)
	  END DO
	END DO

	READ(1,*)NPR1,CHPR1,ZPR1
	READ(1,*)NPR2,CHPR2,ZPR2
	READ(1,*)NPR3,CHPR3,ZPR3

	READ(1,*)ISPLOT
	READ(1,*)NPATH
	DO KK=1,NPATH
	READ(1,*)NPPATH(KK)
	DO JJ=1,NPPATH(KK)
	READ(1,*)IPPATH(KK,JJ)
	END DO
	END DO

	READ(1,*)NPRNT
	DO JJ = 1,NPRNT
        READ(1,*)NPPRNT(JJ),CHPRNT(JJ)
	END DO

	READ(1,*)CODSIM
	IF(CODSIM.EQ."YES")THEN
        READ(1,*)TMAX
	END IF

	READ(1,*)CODECS	
	IF(CODECS.EQ."YES")THEN
	READ(1,*)NCS
	DO I=1,NCS
	 READ(1,*)NPCS(I),CHCS(I),PLCS(I)
	END DO
	END IF

	READ(1,*)CODEPR
	IF(CODEPR.EQ."YES")THEN

	READ(1,*)CODEAC
        IF(CODEAC.EQ."YES")THEN
        READ(1,*)NPAC,GLAC,ACC,DCAC,ACNRV,KACTYP,DORBY
	END IF
	READ(1,*)NOSTD
	IF(NOSTD.GT.0)THEN
	  DO I = 1,NOSTD
          READ(1,*)NPOST(I),CHOST(I),GLOST(I),STGOST(I),DOST(I),
     *             YSTOST(I),DCOST(I)
	  END DO
        END IF

        READ(1,*)NZVD
        IF(NZVD.GT.0)THEN
        DO I = 1,NZVD
         READ(1,*)NPZVV(I),CHZVV(I),DBYCV(I)
        END DO
        END IF
	READ(1,*)NDPCVD
	IF(NDPCVD.GT.0)THEN
	  DO I  = 1,NDPCVD
          READ(1,*)NPDPCV(I),CHDPCV(I),DBYCV(I)
	  END DO
	END IF
	READ(1,*)NNRVD
	IF(NNRVD.GT.0)THEN
	 DO I = 1,NNRVD
         READ(1,*)NPINRV(I),CHINRV(I),DBYNRV(I),DLYRV(I)
	 END DO
	END IF
	READ(1,*)NAVD
	IF(NAVD.GT.0)THEN
	 DO I=1,NAVD
         READ(1,*)NPAIRV(I),SZAIRV(I),CHAIRV(I),AVINV(I)
	 END DO
	END IF
	READ(1,*)NACVD
	IF(NACVD.GT.0)THEN 
	 DO I = 1,NACVD
	 READ(1,*)NPACV(I),SZACV(I),CHACV(I),ACVINV(I)
	 END DO
	END IF
	READ(1,*)NSSD
	IF(NSSD.GT.0)THEN
	 DO I = 1,NSSD
  	 READ(1,*)NPSS(I),CHSS(I),SSDIA(I),SSINV(I),SSTOP(I)
	 END DO
	END IF
	READ(1,*)NSVD
	IF(NSVD.GT.0)THEN
	 DO I = 1,NSVD
	 READ(1,*)NPSV(I),CHSV(I),NNSV(I),SIZSV(I),SVINV(I),PLOWSV(I),
     *            PHISV(I),CTIMSV(I)
	 END DO
	END IF
	READ(1,*)CODIV
	IF(CODIV.EQ.'YES')THEN
	READ(1,*)NPIV,CHIV,DLYIVD,TCIVD 
	READ(1,*)CODBIV
	IF(CODBIV.EQ.'YES')THEN
	READ(1,*)HDELB,SZBIV,DLYBVD,TOBIVD,TOPGBD,TCBIVD
	ENDIF
	ENDIF	 

	END IF

	DATA PI,G,CDAIRV,CDACV,DX/3.1416,9.81,0.6,0.5,0.0714/

C	STANDARD BFV CHARACS
	DATA TAU/
     *	1.000,0.980,0.960,0.930,0.890,0.850,0.818,0.780,0.740,0.700,
     *  0.670,0.638,0.600,0.558,0.530,0.510,0.465,0.428,0.390,0.358,
     *  0.316,0.290,0.265,0.245,0.230,0.200,0.180,0.160,0.145,0.130,
     *  0.119,0.102,0.098,0.080,0.065,0.059,0.040,0.030,0.018,0.010,
     *  0.000,0.000/

C	OG AND TAUZV FOR ZERO VELOCITY VALVE
        DATA OGZVV/0.00,0.01,0.02,0.03,0.05,0.08,0.12,
     *          0.17,0.22,0.27,0.33,0.40,0.47,0.55,
     *          0.63,0.71,0.78,0.84,0.90,0.96,1.00/
       DATA TAUZVV/0.000,0.031,0.064,0.099,0.139,0.182,0.229,
     *            0.283,0.341,0.412,0.492,0.583,0.697,0.775,
     *            0.830,0.900,0.930,0.960,0.985,0.995,1.000/

C	OG AND TAUZV FOR DUAL PLATE CHECK VALVE
        DATA OGDP/0.00,0.09,0.18,0.25,0.30,0.34,0.37,
     *          0.39,0.41,0.43,0.45,0.46,0.47,0.48,
     *          0.50,0.52,0.55,0.61,0.72,0.85,1.00/
        DATA TAUDP/0.000,0.040,0.095,0.140,0.195,0.250,0.310,
     *            0.390,0.470,0.550,0.640,0.750,0.840,0.910,
     *            0.960,0.980,0.995,1.000,1.000,1.000,1.000/


        WRITE (5,3)PROJECT
3       FORMAT(/2X,'PROJECT          : ',A60)
        WRITE (5,6)CASE
6       FORMAT(/2X,'ANALYSIS CASE    : ',A60)

	WRITE(5,9)
9	FORMAT(/2X,'TRANSMISSION MAIN DETAILS:')
	IF(SIML.EQ.'TYPEB')THEN
	DO II=1,NPMAX
	WRITE(5,12)II,IND1(II),IND2(II)
12	FORMAT(/2X,
     *  'PIPE NUMBER                             =',I3/2X,
     *  'UPSTREAM NODE NUMBER                    =',I3/2X,
     *  'DOWNSTREAM NODE NUMBER                  =',I3)
	NPRED=0
	NSUCC=0
	INDUS=IND1(II)
	INDDS=IND2(II)
	DO KK=1,NPMAX
	 IF(IND2(KK).EQ.INDUS)THEN
	 NPRED=NPRED+1
	 IPRED(NPRED)=KK
	 ENDIF
	 IF(IND1(KK).EQ.INDDS)THEN
	 NSUCC=NSUCC+1
	 ISUCC(NSUCC)=KK
	 ENDIF
	END DO
	IF(NPRED.GT.0)THEN
	WRITE(5,13)NPRED
13	FORMAT(2X,
     *  'NUMBER OF PREDECESSOR PIPES             =',I3)
	WRITE(5,15)(IPRED(JJ),JJ=1,NPRED)
15	FORMAT(2X,
     *  'PREDECESSOR PIPE NUMBER/S               =',5I3)
	ENDIF
	IF(NSUCC.GT.0)THEN
	WRITE(5,17)NSUCC
17	FORMAT(2X,
     *  'NUMBER OF SUCCESSOR PIPES               =',I3)
	WRITE(5,18)(ISUCC(JJ),JJ=1,NSUCC)
18	FORMAT(2X,
     *  'SUCCESSOR PIPE NUMBER/S                 =',5I3)
	ENDIF
	WRITE(5,20)PDC(II),PDIA(II),PLEN(II),WV(II)
20	FORMAT(2X,
     *  'DISCHARGE (cum/sec)                     =',F6.3/2X,
     *  'INTERNAL DIAMETER (mm)                  =',F6.0/2X,
     *  'LENGTH (m)                              =',F6.0/2X,
     *  'PRESSURE WAVE VELOCITY (m/sec)          =',F6.0)
	END DO
	ENDIF
	IF(SIML.EQ.'TYPEA')THEN
	WRITE(5,19)
19	FORMAT(2X)
	IF(NPMAX.EQ.1)THEN
	II=1
	ELSE
	II=2
	ENDIF
	WRITE(5,22)PDC(II),PDIA(II),PLEN(II),WV(II)
22	FORMAT(2X,
     *  'DESIGN DISCHARGE (cum/sec)                  =',F6.3/2X,
     *  'INTERNAL DIAMETER OF TRANSMISSION MAIN (mm) =',F6.0/2X,
     *  'LENGTH OF TRANSMISSION MAIN (m)             =',F6.0/2X,
     *  'PRESSURE WAVE VELOCITY (m/sec)              =',F6.0)
	ENDIF
	IF(NPMP.GT.0)THEN
	WRITE(5,25)
25	FORMAT(/2X,'PUMP DETAILS:')
	IF(SIML.EQ.'TYPEA'.OR.(SIML.EQ.'TYPEB'.AND.NPMP.EQ.1))
     *  WRITE(5,19)
	DO II=1,NPMP
	IF(NPMP.GT.1.AND.SIML.EQ.'TYPEB')THEN
	DO KK=1,NNMAX
	 IF(IPMP(KK).EQ.II)NN=KK
	END DO
	WRITE(5,30)II,NN
30	FORMAT(/2X,
     *  'PUMP CLUSTER NUMBER                     =',I3/2X,
     *  'NODE NUMBER                             =',I3/)
	ENDIF
	IF(SIML.EQ.'TYPEA')THEN
	 IF(NPMAX.EQ.1)THEN
	 NWPMPS=NPUMPS(1)
	 ELSE
	 NWPMPS=NPUMPS(1)+NPUMPS(2)
	 ENDIF
	ELSE
	 NWPMPS=NPUMPS(II)
	ENDIF
	IF(SIML.EQ.'TYPEB'.OR.(SIML.EQ.'TYPEA'.AND.II.EQ.1))THEN
	IF(CODPMP(II).EQ.'YES')THEN
	 WRITE(5,35)NWPMPS,PUMPDC(II),QRPMP(II),PUMPH(II),
     *   HRPMP(II),EFFP(II),IPMPSP(II),GD2PMP(II),GD2PM(II),
     *   SUMPWL(II),CODNRR(II),KODPHV(II)
35	 FORMAT(2X,
     * 	 'NUMBER OF WORKING PUMPS                 =',I3/2X,
     *	 'OPERATING DISCHARGE PER PUMP (cum/sec)  =',F6.3/2X,
     *   'RATED DISCHARGE OF PUMP (cum/sec)       =',F6.3/2X,
     *   'OPERATING PUMP HEAD (m)                 =',F6.1/2X,
     *   'RATED PUMP HEAD (m)                     =',F6.1/2X,
     *   'RATED PUMP EFFICIENCY (%)               =',F5.1/2X,
     *   'RATED SPEED OF PUMP (rpm)               =',I5,/2X,
     *   'GD-SQUARE OF THE PUMP (kgf-sqm)         =',F6.2/2X,
     *   'GD-SQUARE OF THE MOTOR (kgf-sqm)        =',F6.2/2X,
     *   'WATER LEVEL IN THE SUMP (RL,m)          =',F7.2/2X,
     *   'NON-REVERSE ROTATION RATCHET PROVIDED?  =',1X,A3/2X,
     *   'CODE FOR TYPE OF NON-RETURN VALVE       =',I2)
	ELSE
	 WRITE(5,40)NWPMPS,PUMPDC(II),PUMPH(II),IPMPSP(II),
     *   SUMPWL(II),KODPHV(II)
40	 FORMAT(2X,
     * 	 'NUMBER OF WORKING PUMPS                 =',I3/2X,
     *	 'DISCHARGE PER PUMP (cum/sec)            =',F6.3/2X,
     *   'PUMP HEAD (m)                           =',F6.1/2X,
     *   'SPEED OF PUMP (rpm)                     =',I5,/2X,
     *   'WATER LEVEL IN THE SUMP (RL,m)          =',F7.2/2X,
     *   'CODE FOR TYPE OF NON-RETURN VALVE       =',I3)
	ENDIF
	IF(KODPHV(II).EQ.1)THEN            
	WRITE (5,43)DLYPH(II)
43	FORMAT(2X,
     *  'DELAY IN CLOSURE OF NRV (sec)           =',F5.2) 
	ELSEIF(KODPHV(II).EQ.2)THEN         
	WRITE (5,46)TCLOSE(II),DLYPH(II)
46	FORMAT(2X,
     *  'TIME OF CLOSURE (sec)                   =',F7.2/2X,
     *  'DELAY IN CLOSURE (sec)                  =',F7.2)
	ELSEIF(KODPHV(II).EQ.3)THEN
	WRITE (5,49)TRAPID(II),TSLOW(II),DLYPH(II)
49	FORMAT(2X,
     *  'TIME OF FIRST 90 % CLOSURE (sec)        =',F7.2/2x,
     *  'TIME OF LAST 10 % CLOSURE (sec)         =',F7.2/2x,
     *  'DELAY IN CLOSURE (sec)                  =',F7.2)
	ELSEIF(KODPHV(II).EQ.4)THEN
	WRITE (5,52)TCLOSE(II)
52	FORMAT(2X,
     *  'TIME OF CLOSURE (sec)                   =',F7.2)
	ELSEIF(KODPHV(II).EQ.5)THEN
	WRITE (5,55)TRAPID(II),TSLOW(II)
55	FORMAT(2X,
     *  'TIME OF FIRST 90 % CLOSURE (sec)        =',F7.2/2X,
     *  'TIME OF LAST 10 % CLOSURE (sec)         =',F7.2)
	ENDIF
	IF(NPMP.GT.1.AND.SIML.EQ.'TYPEB')THEN
	WRITE(5,57)ITRIP(II)
57	FORMAT(2X,
     *  'PUMP TRIP CODE                          =',I2/2X,
     *  '[Running Pump:0; Tripping Pump:1]')
	ENDIF
	ENDIF
	END DO
	ENDIF
	IF(SIML.EQ.'TYPEA'.AND.NPMAX.GT.1)THEN
	WRITE(5,60)PDIA(1),WV(1)
60	FORMAT(/2X,
     *  'PUMP DELIVERY PIPE DIAMETER (mm)                ='F5.0/2X,
     *  'PRESSURE WAVE VELOCITY IN DELIVERY PIPE (m/sec) ='F6.0/2X)
	ENDIF

	IF(NBST.GT.0)THEN
	WRITE(5,65)
65	FORMAT(/2X,'BOOSTER PUMP DETAILS:'/)
	DO II=1,NBST
	DO KK=1,NNMAX
	 IF(IBST(KK).EQ.II)NN=KK
	END DO
	WRITE(5,70)II,NN
70	FORMAT(2X,
     *  'BOOSTER PUMP CLUSTER NUMBER             =',I3/2X,
     *  'NODE NUMBER                             =',I3/)
	IF(CODBST(II).EQ.'YES')THEN
	 WRITE(5,75)NBOOST(II),BSTDC(II),QRBST(II),BSTH(II),
     *   HRBST(II),EFFB(II),IBSTSP(II),GD2BST(II),GD2BM(II),
     *   HGLSUC(II),CODNRB(II),ITRIPB(II),KODBSV(II)
75	 FORMAT(2X,
     * 	 'NUMBER OF WORKING PUMPS                 =',I3/2X,
     *	 'OPERATING DISCHARGE PER PUMP (cum/sec)  =',F6.3/2X,
     *   'RATED DISCHARGE OF PUMP (cum/sec)       =',F6.3/2X,
     *   'OPERATING PUMP HEAD (m)                 =',F6.1/2X,
     *   'RATED PUMP HEAD (m)                     =',F6.1/2X,
     *   'RATED PUMP EFFICIENCY (%)               =',F5.1/2X,
     *   'RATED SPEED OF PUMP (rpm)               =',I5,/2X,
     *   'GD-SQUARE OF THE PUMP (kgf-sqm)         =',F6.2/2X,
     *   'GD-SQUARE OF THE MOTOR (kgf-sqm)        =',F6.2/2X,
     *   'HGL ELEVATION ON SUCTION SIDE (RL,m)    =',F7.2/2X,
     *   'NON-REVERSE ROTATION RATCHET PROVIDED?  =',1X,A3/2X,
     *   'PUMP TRIP CODE                          =',I2/2X,
     *   '[Running Pump:0; Tripping Pump:1]'/2X,
     *   'CODE FOR TYPE OF NON-RETURN VALVE       =',I2)
	ELSE
	 WRITE(5,77)NBOOST(II),BSTDC(II),BSTH(II),IBSTSP(II),
     *   HGLSUC(II),ITRIPB(II),KODBSV(II)
77	 FORMAT(2X,
     * 	 'NUMBER OF WORKING PUMPS                 =',I3/2X,
     *	 'DISCHARGE PER PUMP (cum/sec)            =',F6.3/2X,
     *   'PUMP HEAD (m)                           =',F6.1/2X,
     *   'SPEED OF PUMP (rpm)                     =',I5,/2X,
     *   'HGL ELEVATION ON SUCTION SIDE (RL,m)    =',F7.2/2X,
     *   'PUMP TRIP CODE                          =',I2/2X,
     *   'CODE FOR TYPE OF NON-RETURN VALVE       =',I2)
	ENDIF
	IF(KODBSV(II).EQ.1)THEN            
	WRITE (5,43)DLYPH(II)
	ELSEIF(KODBSV(II).EQ.2)THEN         
	WRITE (5,46)TCLOSE(II),DLYPH(II)
	ELSEIF(KODBSV(II).EQ.3)THEN
	WRITE (5,49)TRAPID(II),TSLOW(II),DLYPH(II)
	ELSEIF(KODBSV(II).EQ.4)THEN
	WRITE (5,52)TCLOSE(II)
	ELSEIF(KODBSV(II).EQ.5)THEN
	WRITE (5,55)TRAPID(II),TSLOW(II)
	ENDIF
	END DO
	ENDIF

	IF(NSOU.GT.0)THEN
	WRITE(5,80)
80	FORMAT(/2X,'SOURCE RESERVOIR DETAILS:'/)
	DO II=1,NSOU
	DO KK=1,NNMAX
	 IF(ISOU(KK).EQ.II)NN=KK
	END DO 
	WRITE(5,85)NN,SOUDC(II),SOUWL(II)
85	FORMAT(2X,
     *  'NODE NUMBER                             =',I3/2X,
     *  'DISCHARGE FROM THE RESERVOIR (cum/sec)  =',F6.3/2X,
     *  'WATER LEVEL IN THE RESERVOIR (RL,m)     =',F7.2/)
	END DO
	ENDIF

	WRITE(5,95)
95	FORMAT(/2X,'DELIVERY RESERVOIR DETAILS:'/)
	DO II=1,NRES
	IF(SIML.EQ.'TYPEB')THEN
	DO KK=1,NNMAX
	 IF(IRES(KK).EQ.II)NN=KK
	END DO 
	WRITE(5,97)NN
97	FORMAT(2X,
     *  'NODE NUMBER                                    =',I3)
	ENDIF
	WRITE(5,100)RESDC(II),RESWL(II)
100	FORMAT(2X,
     *  'DISCHARGE INTO THE RESERVOIR (cum/sec)         =',F6.3/2X,
     *  'RESERVOIR WATER LEVEL OR DELIVERY LEVEL (RL,M) =',F7.2/)
	END DO

	IF(NCDS.GT.0.OR.NOBS.GT.0)THEN
	WRITE(5,105)
105	FORMAT(/2X,'DETAILS OF CONCENTRATED LOCAL HEAD LOSS:'/)
	DO II=1,NNMAX
	IFLHGL=0
	IF(TYPE(II).EQ.'CDS')THEN
	JCDS=ICDS(II)
	USHGL=HGL1CD(JCDS)
	DSHGL=HGL2CD(JCDS)
	IFLHGL=1
	ELSEIF(TYPE(II).EQ.'OBS')THEN
	JOBS=IOBS(II)
	USHGL=HGL1OB(JOBS)
	DSHGL=HGL2OB(JOBS)
	IFLHGL=1
	ENDIF
	IF(IFLHGL.EQ.1)THEN
	WRITE(5,110)II,USHGL,DSHGL
110	FORMAT(2X,
     *  'NODE NUMBER                             =',I3/2X,
     *  'HGL ELEVATION ON UPSTREAM SIDE (RL,m)   =',F7.2/2X,
     *  'HGL ELEVATION ON DOWNSTREAM SIDE (RL,m) =',F7.2/)
	ENDIF
	END DO
	ENDIF

	IF(CODEAC.EQ.'YES')THEN
	WRITE(5,115)
115	FORMAT(/2X,'DETAILS OF AIR VESSEL'/)
	IF(SIML.EQ.'TYPEB')WRITE(5,117)NPAC
117	FORMAT(2X,
     *	'AIR VESSEL LOCATION PIPE NUMBER         =',I3)
	WRITE(5,120)GLAC,ACC,ACNRV,DCAC,KACTYP
120	FORMAT(2X,
     *  'GROUND LEVEL AT THE LOCATION (RL,m)     =',F8.3/2X,
     *  'SIZE PARAMETER OF THE AIR VESSEL (KAV)  =',F6.2/2X,
     *  'NRV PROVIDED ON THE TRANSMISSION MAIN?  -',1X,A3/2X,
     *  'CONNECTING PIPE SIZE (mm)               =',F5.0/2X,
     *  'TYPE OF AIR VESSEL (1 or 2)             =',I2)
	DCAC=0.001*DCAC
	IF(KACTYP.EQ.1)THEN
	WRITE (5,125)DORBY
125	FORMAT(2X,
     *  'SIZE OF ORIFICE IN CONNECTING PIPE (mm) =',F5.0)
	DORIF=0.001*DORBY
	ELSEIF(KACTYP.EQ.2)THEN
	WRITE (5,130)DORBY
130	FORMAT(2X,
     *  'BYPASS SIZE FOR CONNECTING PIPE (mm)    =',F5.0)
	DBYAC=0.001*DORBY
	ENDIF
	ENDIF

	IF(NOSTD.GT.0)THEN
	WRITE (5,135)NOSTD
135	FORMAT(/2X,
     *  'NUMBER OF ONE WAY SURGE TANKS           =',I2/)
	WRITE(5,140)
140	FORMAT(2X,'DETAILS OF ONE WAY SURGE TANKS:')
	IF(SIML.EQ.'TYPEA')THEN
	WRITE (5,142)
142 	FORMAT(/3X,'SL NO.',2X,'LOCATION',2X,'GROUND LEVEL',2X,
     *  'STAGING',3X,'DIAMETER',2X,'STORAGE',3X,'CONNECTING'/10X,
     *  'Chainage,m',4X,'RL,m',7X,'HEIGHT,m',6X,'m',5X,'DEPTH,m',2X,
     *	 'PIPE SIZE,mm'/)
	DO I=1,NOSTD
	WRITE (5,143) I,CHOST(I),GLOST(I),STGOST(I),DOST(I),
     *  YSTOST(I),DCOST(I)
143	FORMAT(5X,I2,5X,F6.0,5X,F8.3,6X,F5.2,4X,F7.3,3X,F5.2,7X,F5.0)
	DCOST(I)=0.001*DCOST(I)
	END DO
	ELSE
	WRITE (5,145)
145 	FORMAT(/2X,'SL NO',1X,'PIPE NO',2X,'LOCATION',4X,
     *  'GL',3X,'STAGING',2X,'DIAMETER',2X,'STORAGE',
     *  3X,'CONNECTING'/19X,'Ch.,m',4X,'RL,m',2X,'HEIGHT,m',
     *  4X,'m',5X,'DEPTH,m',2X,'PIPE SIZE,mm'/)
	DO I=1,NOSTD
	WRITE (5,150) I,NPOST(I),CHOST(I),GLOST(I),STGOST(I),DOST(I),
     *  YSTOST(I),DCOST(I)
150	FORMAT(3X,I2,4X,I3,5X,F6.0,4X,F7.2,2X,F5.2,2X,F7.3,3X,
     *  F5.2,7X,F5.0)
	DCOST(I)=0.001*DCOST(I)
	END DO
	ENDIF
	ENDIF

	IF(NZVD.GT.0)THEN
	WRITE (5,160)NZVD
160	FORMAT(/2X,
     *  'NUMBER OF ZERO VELOCITY VALVES          =',I3)
	WRITE (5,165) 
165	FORMAT(/2X,'DETAILS OF ZERO VELOCITY VALVES'/)
	IF(SIML.EQ.'TYPEA')THEN
	WRITE (5,167)
167	FORMAT(3X,'SL NO.',2X,'LOCATION',4X,'BYPASS'/
     *  10X,'Chainage,m',2X,'SIZE(mm)'/)
	DO I=1,NZVD 
	WRITE (5,168) I,CHZVV(I),DBYCV(I)
168	FORMAT(4X,I3,5X,F6.0,6X,F5.0)
	DBYCV(I)=0.001*DBYCV(I)
	END DO
	ELSE
	WRITE (5,170)
170	FORMAT(3X,'SL NO.',2X,'PIPE NO.',2X,'LOCATION',4X,'BYPASS'/
     *  19X,'Chainage,m',2X,'SIZE(mm)'/)
	DO I=1,NZVD 
	WRITE (5,175) I,NPZVV(I),CHZVV(I),DBYCV(I)
175	FORMAT(4X,I3,6X,I3,5X,F6.0,6X,F5.0)
	DBYCV(I)=0.001*DBYCV(I)
	END DO
	ENDIF
	ENDIF

	IF(NDPCVD.GT.0)THEN
	WRITE (5,185) NDPCVD
185	FORMAT(/2X,
     *  'NUMBER OF DUAL PLATE CHECK VALVES       =',I3) 
	WRITE (5,190) 
190	FORMAT(/2X,'DETAILS OF DUAL PLATE CHECK VALVES'/)
	IF(SIML.EQ.'TYPEA')THEN
	WRITE (5,167)
	DO I=1,NDPCVD 
	WRITE (5,168) I,CHDPCV(I),DBYCV(I)
	DBYCV(I)=0.001*DBYCV(I)
	END DO
	ELSE
	WRITE (5,170)
	DO I=1,NDPCVD 
	WRITE (5,175) I,NPDPCV(I),CHDPCV(I),DBYCV(I)
	DBYCV(I)=0.001*DBYCV(I)
	END DO
	ENDIF
	ENDIF

	IF(NNRVD.GT.0)THEN
	WRITE (5,200) NNRVD
200	FORMAT(/2X,
     *  'NUMBER OF INTERMEDIATE NONRETURN VALVES =',I3) 
	WRITE (5,205) 
205	FORMAT(/2X,'DETAILS OF INTERMEDIATE NONRETURN VALVES'/)
	IF(SIML.EQ.'TYPEA')THEN
	WRITE (5,207)
207	FORMAT(3X,'SL NO.',2X,'LOCATION',4X,'BYPASS',4X,'DELAY IN'/
     *  10X,'Chainage,m',2X,'SIZE(mm)',2X,'CLOSURE(sec)'/)
	DO I=1,NNRVD 
	WRITE (5,208) I,CHINRV(I),DBYNRV(I),DLYRV(I)
208	FORMAT(4X,I3,5X,F6.0,6X,F5.0,4X,F7.2)
	DBYNRV(I)=0.001*DBYNRV(I)
	END DO
	ELSE
	WRITE (5,210)
210	FORMAT(3X,'SL NO.',2X,'PIPE NO.',2X,'LOCATION',4X,'BYPASS',
     *  4X,'DELAY IN'/
     *  19X,'Chainage,m',2X,'SIZE(mm)',2X,'CLOSURE(sec)'/)
	DO I=1,NNRVD 
	WRITE (5,215) I,NPINRV(I),CHINRV(I),DBYNRV(I),DLYRV(I)
215	FORMAT(4X,I3,6X,I3,5X,F6.0,6X,F5.0,4X,F7.2)
	DBYNRV(I)=0.001*DBYNRV(I)
	END DO
	ENDIF
	ENDIF

	IF(NAVD.GT.0)THEN
	WRITE (5,220) NAVD
220	FORMAT(/2X,
     *  'NUMBER OF AIR VALVES                    =',I3)
	WRITE (5,225)
225	FORMAT(/2X,'DETAILS OF AIR VALVES'/)
	IF(SIML.EQ.'TYPEA')THEN
	WRITE (5,227) 
227	FORMAT(2X,'SL NO.',2X,'SIZE',2X,'LOCATION',2X,'PIPE INVERT'/
     *  11X,'mm',2X,'Chainage,m',2X,'LEVEL RL,m'/)
	DO I=1,NAVD
	WRITE (5,228)I,SZAIRV(I),CHAIRV(I),AVINV(I)
228	FORMAT(3X,I3,4X,F4.0,3X,F6.0,4X,F8.3)
	END DO
	ELSE
	WRITE (5,230) 
230	FORMAT(2X,'SL NO.',2X,'SIZE',2X,'PIPE NO.',2X,'LOCATION',
     *  2X,'PIPE INVERT'/
     *  11X,'mm',11X,'Chainage,m',2X,'LEVEL RL,m'/)
	DO I=1,NAVD
	WRITE (5,235)I,SZAIRV(I),NPAIRV(I),CHAIRV(I),AVINV(I)
235	FORMAT(3X,I3,4X,F4.0,5X,I3,4X,F6.0,4X,F8.3)
	END DO
	ENDIF
	ENDIF

	IF(NACVD.GT.0)THEN
	WRITE (5,240) NACVD
240	FORMAT(/2X,
     *  'NUMBER OF AIR CUSHION VALVES            =',I3)
	WRITE (5,245)
245	FORMAT(/2X,'DETAILS OF AIR CUSHION VALVES'/)
	IF(SIML.EQ.'TYPEA')THEN
	WRITE (5,227) 
	DO I=1,NACVD
	WRITE (5,228)I,SZACV(I),CHACV(I),ACVINV(I)
	END DO
	ELSE
	WRITE (5,230) 
	DO I=1,NACVD
	WRITE (5,235)I,SZACV(I),NPACV(I),CHACV(I),ACVINV(I)
	END DO
	ENDIF
	ENDIF

	IF(NSSD.GT.0)THEN
	WRITE (5,250) NSSD
250	FORMAT(/2X,
     *  'NUMBER OF STAND PIPES                   =',I3)
	WRITE (5,255)
255	FORMAT(/2X,'DETAILS OF STAND PIPES'/)
	IF(SIML.EQ.'TYPEA')THEN
	WRITE (5,257)
257	FORMAT(3X,'SL NO.',3X,'LOCATION'2X,'DIAMETER'2X,'PIPE INVERT',2X,
     *  'TOP LEVEL OF'/11X,'Chainage,m',4X,'mm',6X,'LEVEL RL,m',2X,
     *  'STAND PIPE RL,m'/) 
	DO I=1,NSSD
	WRITE (5,258) I,CHSS(I),SSDIA(I),SSINV(I),SSTOP(I)
258	FORMAT(4X,I3,6X,F6.0,5X,F5.0,4X,F8.3,7X,F8.3)
	SSDIA(I)=0.001*SSDIA(I)
	END DO
	ELSE
	WRITE (5,260)
260	FORMAT(3X,'SL NO.',2X,'PIPE NO.',3X,'LOCATION',2X,'DIAMETER',
     *  2X,'PIPE INVERT',2X,'TOP LEVEL'/20X,'Chainage,m',4X,'mm',6X,
     *  'LEVEL RL,m',2X,'OF PIPE RL,m'/) 
	DO I=1,NSSD
	WRITE (5,265) I,NPSS(I),CHSS(I),SSDIA(I),SSINV(I),SSTOP(I)
265	FORMAT(4X,I3,6X,I3,6X,F6.0,5X,F5.0,4X,F8.3,5X,F8.3)
	SSDIA(I)=0.001*SSDIA(I)
	END DO
	ENDIF
	ENDIF

	IF(NSVD.GT.0)THEN
	WRITE (5,275)
275	FORMAT(/2X,'DETAILS OF SURGE RELIEF VALVES')
	DO I=1,NSVD
	IF(SIML.EQ.'TYPEB')WRITE(5,278)I,NPSV(I),CHSV(I)
278	FORMAT(/2X,
     *  'SURGE RELIEF VALVE NUMBER               =',I3/2X,
     *  'VALVE LOCATION PIPE NUMBER              =',I3/2X,
     *  'CHAINAGE OF LOCATION (m)                =',F6.0)
	WRITE(5,280)NNSV(I),SIZSV(I),SVINV(I),PLOWSV(I),
     *  PHISV(I),CTIMSV(I)
280	FORMAT(2X,
     *  'NUMBER OF VALVES                        =',I3/2X,
     *  'SIZE OF THE VALVE/S (mm)                =',F5.0/2X,
     *  'PIPE INVERT LEVEL AT VALVE ,RL(m)       =',F8.3/2X,
     *  'LOW PRESSURE PILOT SETTING (m)          =',F5.1/2X,
     *  'HIGH PRESSURE PILOT SETTING (m)         =',F5.1/2X,
     *  'CLOSURE TIME OF VALVE (sec)             =',F6.1)   
	END DO
	ENDIF

	IF(CODIV.EQ.'YES')THEN
	WRITE(5,285)NPIV,CHIV,DLYIVD,TCIVD
285	FORMAT(/2X,
     *  'PIPE NUMBER FOR INTERMEDIATE VALVE      =',I3/2X,
     *  'CHAINAGE OF INTERMEDIATE VALVE          =',F6.0/2X,
     *  'DELAY IN START OF VALVE CLOSURE (sec)   =',F7.2/2X,
     *  'TIME OF CLOSURE (sec)                   =',F7.2)
	IF(CODBIV.EQ.'YES')THEN
	WRITE(5,290)HDELB,SZBIV,DLYBVD,TOBIVD,TOPGBD,TCBIVD
290	FORMAT(2X,
     *  'BYPASS OUTLET DISCHARGE LEVEL (RL,m)    =',F8.3/2X,
     *  'SIZE OF BYPASS OUTLET (mm)              =',F5.0/2X,
     *  'DELAY IN BYPASS VALVE OPENING (sec)     =',F7.2/2X,
     *  'TIME OF OPENING OF BYPASS VALVE (sec)   =',F7.2/2X,
     *  'DURATION OF OPEN POSITION OF BYPASS(sec)=',F7.2/2X,
     *  'TIME OF CLOSURE OF BYPASS VALVE (sec)   =',F7.2)
	ENDIF
	ENDIF


	IF((NAVD+NACVD).GT.1000)THEN
	WRITE(5,300)
300	FORMAT(/2X,'*************************************************',
     *         /2X,'THE PROGRAM PROHIBITS THE USE OF MORE THAN THREE ',
     *         /2X,'AIR VALVES/AIR CUSHION VALVES, AS EXTENSIVE USE OF',
     *         /2X,'AIR VALVES FOR CONTROL OF DOWNSURGE SHOULD BE ',
     *         /2X,'DONE ONLY IN EXCEPTIONAL CASES.',
     *         /2X,'*************************************************')
	STOP
	ENDIF


	DO II=1,NPMAX
	PDIA(II)=PDIA(II)*0.001
	END DO
	DO JJ=1,NPMP
	EFFP(JJ)=EFFP(JJ)*0.01
	END DO
	DO JJ=1,NBST
	EFFB(JJ)=EFFB(JJ)*0.01
	END DO
	SZBIV=SZBIV*0.001	

	PLMAX=0
	PLMIN=100000
	SUM=0
	DO II=1,NPMAX
	IF(PLEN(II).GT.PLMAX)THEN
	PLMAX=PLEN(II)
	NPREF=II
	ENDIF
	IF(PLEN(II).LT.PLMIN)PLMIN=PLEN(II)
	SUM= SUM+PLEN(II)
	END DO
	IF(NPMP.GT.0)THEN
	 DATUM=SUMPWL(1)
	 REFH=PUMPH(1)
	ELSEIF(NBST.GT.0)THEN
	 DATUM=SOUWL(1)
	 REFH=BSTH(1)
	ELSE
	 DATUM=SOUWL(1)
	 REFH=SOUWL(1)-RESWL(1)
	ENDIF
	REFV=PDC(NPREF)/(PI*PDIA(NPREF)**2/4)
	REFT=PLEN(NPREF)/WV(NPREF)
	DLMIN=SUM/500
	DLREF=PLEN(NPREF)/200
	DLMAX=P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23*(1.0-BETA)
	 DHOR1=DHOC+ORIFK*(VORIF**2)/(2*G*REFH)*(AR(NPAC)**2)
	 DHOR2=DHOC+2.5*ORIFK*(VORIF**2)/(2*G*REFH)*(AR(NPAC)**2)
	 ELSEIF(KACTYP.EQ.2)THEN
	 VBYAC=PDC(NPREF)/((PI*DBYAC**2)/4)
	 DHOR1=DHOC
	 DHOR2=DHOC+(3*VBYAC**2)/(2*G*REFH)
	 ENDIF
	 IF(ACNRV.EQ.'YES')THEN
	 DLYAC=0.0
	 ELSE
	 DLYAC=1000
	 ENDIF
	 ZAC=GLAC+2.0-DATUM
	 HATC=(10.0-ZAC)/REFH
         TVOAC=10000.0
	ENDIF	

	IF(NOSTD.GT.0)THEN
	 DO I=1,NOSTD
	 CCH=CHOST(I)-CHST(NPOST(I))
	 IOST(I)=(CCH/PLEN(NPOST(I))*NGAP(NPOST(I)))+1
	 IF(IOST(I).EQ.1)IOST(I)=2
	 IF(IOST(I).EQ.NEND(NPOST(I)))IOST(I)=NGAP(NPOST(I))
         OSTC(I)=((REFH/REFV)/REFT)*(DOST(I)/PDIA(NPOST(I)))**2
	 OSTK(I)=OSTC(I)/DT
         VCNP=PDC(NPREF)/((PI*DCOST(I)**2)/4)
	 HLOST(I)=(4*VCNP**2)/(2*G*REFH)*AR(NPOST(I))**2
	 HTOST(I)=(GLOST(I)+YSTOST(I)+STGOST(I)-DATUM)/REFH
	 OSTBED(I)=HTOST(I)-(YSTOST(I)/REFH)	 
	 HOST(I)=HTOST(I)
	 HOSTO(I)=HOST(I)
	 TDOST(I)=1000
	 TOSTS(I)=1000
	 TOSTE(I)=1000
	 ENDDO
	ENDIF

	IF(NZVD.GT.0)THEN
	 DO I=1,NZVD
	 CCH=CHZVV(I)-CHST(NPZVV(I))
	 IZV(I)=(CCH/PLEN(NPZVV(I)))*NGAP(NPZVV(I))+1
	 IF(IZV(I).EQ.1)IZV(I)=2
	 IF(IZV(I).EQ.NEND(NPZVV(I)))IZV(I)=NGAP(NPZVV(I))
	 DHZ(I)=(2*REFV**2)/(2*G*REFH)
	 IF(DBYCV(I).GT.0.01)THEN
	 VBYZV=((PDIA(NPZVV(I))/DBYCV(I))**2)*REFV
	 DHB(I)=(3*VBYZV**2)/(2*G*REFH)
	 ELSE
	 DHB(I)=1000
	 ENDIF
         IF(DHB(I).LT.999)DHZB(I)=SQRT(DHZ(I)/DHB(I))
	 ENDDO
	ENDIF

        IF(NDPCVD.GT.0)THEN
	 DO I=1,NDPCVD
	 CCH=CHDPCV(I)-CHST(NPDPCV(I))
	 IZV(I)=(CCH/PLEN(NPDPCV(I)))*NGAP(NPDPCV(I))+1
	 IF(IZV(I).EQ.1)IZV(I)=2
	 IF(IZV(I).EQ.NEND(NPDPCV(I)))IZV(I)=NGAP(NPDPCV(I))
	 DHZ(I)=(2*REFV**2)/(2*G*REFH)
	 IF(DBYCV(I).GT.0.01)THEN
	 VBYDP=((PDIA(NPDPCV(I))/DBYCV(I))**2)*REFV
	 DHB(I)=(3*VBYDP**2)/(2*G*REFH)
	 ELSE
	 DHB(I)=1000
	 ENDIF
         IF(DHB(I).LT.999)DHZB(I)=SQRT(DHZ(I)/DHB(I))
	 ENDDO
	ENDIF

	IF(NNRVD.GT.0)THEN
	 DO I=1,NNRVD
	 CCH=CHINRV(I)-CHST(NPINRV(I))
	 INRV(I)=(CCH/PLEN(NPINRV(I)))*NGAP(NPINRV(I))+1
	 IF(INRV(I).EQ.1)INRV(I)=2
	 IF(INRV(I).EQ.NEND(NPINRV(I)))INRV(I)=NGAP(NPINRV(I))
	 DLYNRV(I)=DLYRV(I)/REFT               
	 IF(DBYNRV(I).GT.0.01)THEN
	 VBYNRV=((PDIA(NPINRV(I))/DBYNRV(I))**2)*REFV
	 DHBNRV(I)=(3*VBYNRV**2)/(2*G*REFH)
	 ELSE
	 DHBNRV(I)=1000
	 ENDIF
	 TVONRV(I)=10000
	 ENDDO
	ENDIF

	IF(NSVD.GT.0)THEN
	 DO I=1,NSVD
	 CCH=CHSV(I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)=0
         VCAV(I)=0.0
	 END DO
	ENDIF

	DATA DEVICE/
     *  'AIR VESSEL',
     *  'ONE WAY SURGE TANK',
     *  'ZERO VELOCITY VALVE', 
     *  'DUAL PLATE CHECK VALVE',
     *  'INTERMEDIATE NONRETURN VALVE',
     *  'AIR VALVE', 
     *  'AIR CUSHION VALVE',
     *  'STAND PIPE',
     *  'SURGE RELIEF VALVE',
     *  'COLUMN SEPERATION LOCATION',
     *  'INTERMEDIATE VALVE WITH/WITHOUT BYPASS OUTLET'/

	IFSTOP=0
	IPTOST=0
	IPTZV=0
	IPTNRV=0
	IPTAV=0
	IPTACV=0
	IPTSS=0
	IPTSV=0
	IPTCS=0
	DO IIP=1,NPMAX
	DO II=2,NGAP(IIP)
	NDEV=0

	IF(CODEAC.EQ.'YES')THEN
	 IF(NPAC.EQ.IIP.AND.IAC.EQ.II)THEN
	 NDEV=NDEV+1
	 KODEDV(NDEV)=1
	 ENDIF
	ENDIF
	IF(NOSTD.GT.0)THEN
	 DO KK=1,NOSTD
	 IF(NPOST(KK).EQ.IIP.AND.IOST(KK).EQ.II)THEN
	 IPTOST=IPTOST+1
	 IOROST(IPTOST)=KK
	 NDEV=NDEV+1
	 KODEDV(NDEV)=2
	 NSN(NDEV)=KK
	 CHDEV(NDEV)=CHOST(KK)
	 ENDIF
	 END DO
	ENDIF
	IF(NZVD.GT.0)THEN
	 DO KK=1,NZVD
	 IF(NPZVV(KK).EQ.IIP.AND.IZV(KK).EQ.II)THEN
	 IPTZV=IPTZV+1
	 IORZV(IPTZV)=KK
	 NDEV=NDEV+1
	 KODEDV(NDEV)=3
	 NSN(NDEV)=KK
	 CHDEV(NDEV)=CHZVV(KK)
	 ENDIF
	 END DO
	ENDIF
	IF(NDPCVD.GT.0)THEN
	 DO KK=1,NDPCVD
	 IF(NPDPCV(KK).EQ.IIP.AND.IZV(KK).EQ.II)THEN
	 IPTZV=IPTZV+1
	 IORZV(IPTZV)=KK
	 NDEV=NDEV+1
	 KODEDV(NDE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HSV(KK)
	 ENDIF
	 END DO
	ENDIF
	IF(CODECS.EQ.'YES')THEN
	 DO KK=1,NCS
	 IF(NPCS(KK).EQ.IIP.AND.ICS(KK).EQ.II)THEN
	 IPTCS=IPTCS+1
	 IORCS(IPTCS)=KK
	 NDEV=NDEV+1
	 KODEDV(NDEV)=10
	 NSN(NDEV)=KK
	 CHDEV(NDEV)=CHCS(KK)
	 ENDIF
 	 END DO
	ENDIF
	IF(CODIV.EQ.'YES')THEN
	 IF(NPIV.EQ.IIP.AND.IIV.EQ.II)THEN
	 NDEV=NDEV+1
	 KODEDV(NDEV)=11
	 CHDEV(NDEV)=CHIV
	 ENDIF
	ENDIF

	IF(NDEV.GT.1)THEN
	WRITE(5,400)
400	FORMAT(/2X,
     *  'THE FOLLOWING DEVICES ARE LOCATED TOO CLOSE TO EACH'/2X,
     *  'OTHER FOR MODELLING. PLEASE RELOCATE THEM WITH A'/2X,
     *  'SLIGHTLY LARGER SPACING BETWEEN THEM.'/)
	DO JJ=1,NDEV
	IF(KODEDV(JJ).EQ.1)THEN
	WRITE(5,410)DEVICE(KODEDV(JJ)),IIP
410	FORMAT(2X,A60/2X,
     *  'PIPE NUMBER                   =',I3/)
	ELSE
	WRITE(5,420)DEVICE(KODEDV(JJ)),IIP,CHDEV(JJ)
420	FORMAT(2X,A60/2X,
     *  'PIPE NUMBER                   =',I3/2X,
     *  'CHAINAGE (m)                  =',F6.0/)
	ENDIF
	END DO
	IFSTOP=1
	ENDIF	

	END DO
	END DO
	IF(IFSTOP.EQ.1)THEN
	STOP
	ENDIF

	DO I=1,NPRNT
	KK=NPPRNT(I)
	CCH=CHPRNT(I)-CHST(KK) 
	IPRNT(I)=(CCH/PLEN(KK))*NGAP(KK)+1.5
	CHNODE(I)=FLOAT(IPRNT(I)-1)/NGAP(KK)*PLEN(KK)+CHST(KK)
	NNPRNT(I)=NSTART(NPPRNT(I))+IPRNT(I)-1
	END DO
	NST1=NSTART(NPR1)
	NST2=NSTART(NPR2)
	N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ENDIF


 	WRITE (4,570) CASE
570	FORMAT (/2X,A80)
	WRITE (4,580)NPREF,DATUM,REFH,REFV,REFT
580	FORMAT (/2X,'NPREF=',I3,2X,'DATUM=',F6.1,2X,'REFH=',F6.1,
     *  2X,'REFV=',F6.3,2X,'REFT=',F7.3)
	WRITE(4,583)
583	FORMAT(/2X,'NODE NO',3X,'TYPE',4X,'NUSP',4X,'NDSP'/)
	DO I=1,NNMAX
	WRITE(4,585)I,TYPE(I),NUSP(I),NDSP(I)
585	FORMAT(3X,I3,6X,A3,4X,I3,5X,I3)
	END DO
	WRITE(4,19)
	DO I=1,NNMAX
	IF(NUSP(I).GT.0)WRITE(4,587)I,(IUSP(I,JJ),JJ=1,NUSP(I))
587	FORMAT(2X,'NODE NO. AND IUSP',6I5)
	IF(NDSP(I).GT.0)WRITE(4,588)I,(IDSP(I,JJ),JJ=1,NDSP(I))
588	FORMAT(2X,'NODE NO. AND IDSP',6I5)
	END DO
        WRITE (4,590)
590     FORMAT(/2X,'PIPE NO',3X,'IND1',3X,'IND2',3X,'B',8X,'HF',
     *  6X,'VI',6X,'NGAP',4X,'AR'/)
        DO I=1,NPMAX
        WRITE (4,600) I,IND1(I),IND2(I),B(I),HF(I),VI(I),NGAP(I),AR(I)
600     FORMAT(2X,I4,5X,I3,4X,I3,2X,F8.4,2X,F6.4,2X,F8.6,2X,I4,2X,F5.3)
	END DO
	IF(NPMP.GT.0)THEN
	WRITE (4,610)
610	FORMAT (/2X,'IP',1X,'KODPHV',2X,'ZPU',2X,'HRATED',3X,'PUMPK',
     *  4X,'DHO',4X,'DLY',4X,'TC',5X,'TC1',4X,'TC2',2X,'FKNRR',2X,
     *  'ITRIP'/)
	DO IP=1,NPMP
	WRITE (4,615)IP,KODPHV(IP),ZPU(IP),HRAT(IP),PUMPK(IP),
     *  DHO(IP),DLY(IP),TC(IP),TC1(IP),TC2(IP),FKNRR(IP),ITRIP(IP)
615	FORMAT (2X,I2,3X,I2,3X,F5.3,1X,F6.3,2X,F6.3,2X,F5.3,1X,F6.3,1X,
     *  F6.3,1X,F6.3,1X,F6.3,1X,F6.3,5X,I1)
	END DO
	ENDIF
	IF(NBST.GT.0)THEN
	WRITE (4,618)
618	FORMAT (/2X,'IB',2X,'KODBSV',2X,'HRATED',3X,'BSTK',3X,'DHOB',
     *  4X,'DLYB',3X,'TCB'3X,'TC1B',3X,'TC2B',2X,'FKNRRB',2X,'ITRIPB'/)
	DO IB=1,NBST
	WRITE (4,620)IB,KODBSV(IB),HRATB(IB),BSTK(IB),DHOB(IB),
     *  DLYB(IB),TCB(IB),TC1B(IB),TC2B(IB),FKNRRB(IB),ITRIPB(IB)
620	FORMAT (2X,I2,6X,I2,3X,F6.3,2X,F6.3,2X,F5.3,2X,F6.3,2X,
     *  F6.3,2X,F6.3,2X,F6.3,2X,F6.3,4X,I1)
	END DO
	ENDIF
	WRITE(4,625)
625	FORMAT(/2X,'RES NO',2X,'KODEDS',3X,'TOPDS',3X,
     *  'DLYDS',5X,'HLV'4X,'HGLR'/)
	DO II=1,NRES
	WRITE(4,630)II,KODEDS,TOPDS,DLYDS,HLV,HGLR(II)
630	FORMAT(5X,I2,7X,I2,2X,F6.3,2X,F6.3,2X,F6.4,2X,F6.3)
	END DO
	IF(NSOU.GT.0)THEN
	WRITE(4,635)
635	FORMAT(/2X,'SOURCE RES NO',4X,'HSOU'/)
	DO II=1,NSOU
	WRITE(4,640)II,HSOU(II)
640	FORMAT(9X,I2,9X,F6.3)
	END DO
	ENDIF
	IF(CODEAC.EQ.'YES')THEN
        WRITE (4,645) ACC,DHOR1,DHOR2,P,IAC,DLYAC,HATC,NPAC
645     FORMAT(/2X,'ACC=',F6.3,3X,'DHOR1=',F7.4,3X,'DHOR2=',F7.4,
     *  3X,'P=',F4.2,3X,'IAC=',I4,3X,'DLYAC=',F9.3,3X,'HATC=',F7.4,
     *  3X,'NPAC=',I3)
	ENDIF
	IF(NOST.GT.0)THEN
	WRITE (4,650)
650	FORMAT(/2X,'NPOST',2X,'IOST',5X,'OSTC',3X,'HLOST',3X,
     *  'HTOST',2X,'OSTBED'/)
	DO I=1,NOST
	WRITE (4,655)NPOST(I),IOST(I),OSTC(I),HLOST(I),HTOST(I),
     *  OSTBED(I)
655	FORMAT(2X,I3,4X,I4,2X,F7.2,2X,F6.4,2X,F6.4,2X,F6.4)
	END DO
	ENDIF
	IF(NZV.GT.0)THEN
        WRITE (4,660)
660     FORMAT(/2X,'NPZV/NPDPCV'2X,'IZV/DPCV',4X,'DHZ',7X,'DHB'/)
        DO I=1,NZV
        WRITE (4,665) NPZVV(I),IZV(I),DHZ(I),DHB(I)
	END DO
	ENDIF
	IF(NDPCV.GT.0)THEN
        WRITE (4,660)
        DO I=1,NDPCV
        WRITE (4,665) NPDPCV(I),IZV(I),DHZ(I),DHB(I)
665     FORMAT(6X,I3,8X,I3,5X,F6.4,4X,F8.4)
	END DO
	ENDIF
	IF(NNRV.GT.0)THEN
        WRITE (4,670)
670     FORMAT(/5X,'NPNRV'2X,'INRV',5X,'DHBNRV',7X,'DLYNRV'/)
        DO I=1,NNRV
        WRITE (4,675)NPINRV(I),INRV(I),DHBNRV(I),DLYNRV(I)
675     FORMAT(5X,I3,4X,I3,5X,F8.4,5X,F6.3)
	END DO
	ENDIF
	IF(NSV.GT.0)THEN
        WRITE(4,680)
680     FORMAT(/2X,'NPSV',3X,'ISV',5X,'ZSV',7X,'CSV',6X,'TCSV',5X,
     *  'HMNSV',5X,'HMXSV',7X,'AIRC'/)
        DO I=1,NSV
        WRITE(4,685)NPSV(I),ISV(I),ZSV(I),CSV(I),TCSV(I),HMNSV(I),
     *  HMXSV(I),AIRC(I)
685     FORMAT(3X,I3,3X,I3,6F10.5) 
	END DO
	ENDIF
	IF(NAV.GT.0)THEN
        WRITE (4,690)
690     FORMAT(/2X,'NPAIRV',2X,'IAV',4X,'ZAV',4X,'AVC'/)
        DO I=1,NAV
        WRITE (4,695)NPAIRV(I),IAV(I),ZAV(I),AVC(I)
695     FORMAT(4X,I3,3X,I3,2X,F5.3,3X,F6.3)
	END DO
	ENDIF
	IF(NACV.GT.0)THEN
        WRITE (4,700)
700     FORMAT(/2X,'NPACV',2X,'IACV',4X,'ZACV',4X,'ACVC'/)
        DO I=1,NACV
        WRITE (4,695)NPACV(I),IACV(I),ZACV(I),ACVC(I)
	END DO
	ENDIF
	IF(NSS.GT.0)THEN
	WRITE (4,710)
710	FORMAT(/2X,'NPSS',2X,'ISS',4X,'SSC',5X,'HTSS',5X,'ZSS'/)
	DO I=1,NSS
	WRITE (4,715)NPSS(I),ISS(I),SSC(I),HTSS(I),ZSS(I)
715	FORMAT(3X,I3,3X,I3,2X,F7.3,2X,F6.4,2X,F6.4)
	END DO
	ENDIF
	IF(CODIV.EQ.'YES')THEN
	WRITE(4,717)NPIV,IIV,HLIV,DLYIV,TCIV
717	FORMAT(/2X,'NPIV=',I2,2X,'IIV=',I3,2X,'HLIV=',F6.3,2X,
     *  'DLYIV=',F8.3,2X,'TCIV=',F8.3)
	IF(CODBIV.EQ.'YES')THEN
	WRITE(4,718)HBIV,HLBIV,DLYBIV,TOBIV,TOPGB,TCBIV
718	FORMAT(2X,'HBIV=',F7.3,2X,'HLBIV=',F7.3,2X,'DLYBIV=',F8.3,2X,
     *  'TOBIV=',F8.3,2X,'TOPGB=',F8.3,2X,'TCBIV=',F8.3)
	ENDIF
	ENDIF
	IF(CODECS.EQ.'YES')THEN
	DO I=1,NCS
        WRITE (4,720)NPCS(I),ICS(I),ZCS(I)
720     FORMAT(/2X,'NPCS=',I3,2X,'ICS=',I3,2X,'ZCS=',F5.3)
	END DO
	ENDIF
	WRITE(4,723)JPC,JMAX
723	FORMAT(/2X,'JPC=',I5,2X,'JMAX=',I5/2X,'**********',
     *  '***********************************************************')

	DO I=1,21
	OG(I)=OGZVV(I)
	TAUZV(I)=TAUZVV(I)
	IF(NDPCVD.GT.0)THEN
	OG(I)=OGDP(I)
	TAUZV(I)=TAUDP(I)
	ENDIF
	END DO 
	IF(NDPCVD.GT.0)THEN
	NZV=NDPCVD
	DO II=1,NZV
	NPZVV(II)=NPDPCV(II)
	END DO
	ENDIF

        DATA TOLAC,TOLIV/0.0001,0.0002/


        T=0.0
        J=0
        JPCR=JPC
	NZVC=1
	DO II=1,NPMAX
	NN=NSTART(II)	
	VD(NN)=VI(II)
	HD(NN)=(HGL1(II)-DATUM)/REFH
	VU(NN)=VD(NN)
	HU(NN)=HD(NN)
	IGN1=IND1(II)
	IF(TYPE(IGN1).EQ.'SOU')THEN
	 JJ=ISOU(IGN1)
	 HU(NN)=HSOU(JJ)
	ELSEIF(TYPE(IGN1).EQ.'PMP')THEN
	 JJ=IPMP(IGN1)
	 HU(NN)=ZPU(JJ)+HPU(JJ)*HRAT(JJ)
	ENDIF
	DO KK=2,NEND(II)
	NN=NSTART(II)+KK-1
	VU(NN)=VI(II)
	VD(NN)=VI(II)
	HU(NN)=HD(NN-1)-R(II)*VI(II)**2
	IF(NZV.GT.0.AND.NPZVV(NZVC).EQ.II.AND.IZV(NZVC).EQ.KK)THEN
	 HD(NN)=HU(NN)-DHZZ*VI(II)**2
	 NZVC=NZVC+1
	ELSE
	 HD(NN)=HU(NN)
	ENDIF
	IGN2=IND2(II)
	IF(KK.EQ.NEND(II).AND.TYPE(IGN2).EQ.'RES')THEN
	 JJ=IRES(IGN2)
	 HD(NN)=HGLR(JJ)
	ENDIF
	END DO	 
	END DO

	IF(NSS.GT.0)THEN
	NSSC=1
	DO I=1,NSS
	II=NPSS(NSSC)
	KK=ISS(NSSC)
	NN=NSTART(II)+KK-1
	HSS(I)=HU(NN)
	NSSC=NSSC+1
	END DO
	ENDIF
	IF(CODEAC.EQ.'YES')THEN
	NN=NSTART(NPAC)+1
        HACIC=HD(NN)+HATC
        HAC=HD(NN)
	ENDIF
	DO II=1,NPMAX
        DO KK=1,NEND(II)
	NN=NSTART(II)+KK-1
        HUMAX(NN)=HU(NN)
        HDMAX(NN)=HD(NN)
        HUMIN(NN)=HU(NN)
        HDMIN(NN)=HD(NN)
        VUMAX(NN)=VU(NN)
        VDMAX(NN)=VD(NN)
        VUMIN(NN)=VU(NN)
        VDMIN(NN)=VD(NN)
	END DO
	END DO

	IF(NSS.GT.0)THEN
	DO I=1,NSS
	HSSMAX(I)=HSS(I)
	HSSMIN(I)=HSS(I)
	END DO
	ENDIF
	IF(NPMP.GT.0)THEN
	DO IP=1,NPMP
        ALMIN(IP)=AL(IP)
        ALMAX(IP)=AL(IP)
        BETMIN(IP)=BET(IP)
        BETMAX(IP)=BET(IP)
        HPUMIN(IP)=HPU(IP)
        HPUMAX(IP)=HPU(IP)
        VPUMIN(IP)=VPU(IP)
        VPUMAX(IP)=VPU(IP)
	END DO
	ENDIF
	IF(NBST.GT.0)THEN
	DO IB=1,NBST
        ALMINB(IB)=ALB(IB)
        ALMAXB(IB)=ALB(IB)
        BETMNB(IB)=BETB(IB)
        BETMXB(IB)=BETB(IB)
        HPUMNB(IB)=HPUB(IB)
        HPUMXB(IB)=HPUB(IB)
        VPUMNB(IB)=VPUB(IB)
        VPUMXB(IB)=VPUB(IB)
	END DO
	ENDIF
	IF(CODECS.EQ.'YES')THEN
	DO I=1,NCS
        VCAVMX(I)=VCAV(I)
        DVCVMX(I)=0.0
        DVCVMN(I)=0.0
	END DO
	ENDIF
	IF(CODEAC.EQ.'YES')THEN
        CMAX=CO
        CMIN=CO
        HACMAX=HAC
        HACMIN=HAC
        VACMAX=VAC
        VACMIN=VAC
	ENDIF
	IF(NOST.GT.0)THEN
	DO I=1,NOST
        HOSTMX(I)=HOST(I)
        HOSTMN(I)=HOST(I)
	VOSTMX(I)=0.0
 	END DO
	ENDIF
	IF(NSV.GT.0)THEN
  	DO I=1,NSV
	VSVMN(I)=VSV(I)
	VSVMX(I)=VSV(I)
	END DO
	ENDIF

	IF(REFT.LT.2)THEN
	JRATE=10/(DT*REFT)
	ELSEIF(REFT.GT.40)THEN
	JRATE=200/(DT*REFT)
	ELSE
	JRATE=5/DT
	ENDIF	 
	TRATE=JRATE*DT*REFT
	IRATE=TRATE/10+0.99
	XXRATE=IRATE*10
	XNRATE=0
	IF(XXRATE.LE.10)THEN
	DXRATE=1
	ELSEIF(XXRATE.GT.10.AND.XXRATE.LE.20)THEN
	DXRATE=2
	ELSEIF(XXRATE.GT.20.AND.XXRATE.LE.50)THEN
	DXRATE=4
	ELSEIF(XXRATE.GT.50.AND.XXRATE.LE.100)THEN
	DXRATE=10
	ELSEIF(XXRATE.GT.100.AND.XXRATE.LE.200)THEN
	DXRATE=20
	ELSE
	DXRATE=50
	ENDIF
	DYRATE=5
	YNRATE=-10
	YXRATE=REFH
	IF(REFH.GT.90)YXRATE=90	
	
	JSTAV=20/DT
	TSTAV=JSTAV*DT*REFT
	IF(TSTAV.LT.20)THEN
	TSTAV=20
	JSTAV=TSTAV/(DT*REFT)
	ENDIF
	ISTAV=TSTAV/10+0.99
	XXSTAV=ISTAV*10

	WRITE(7,990)XNRATE,XXRATE,DXRATE
990	FORMAT(40X,3F10.3)
995	FORMAT(20X,3F10.3)
	WRITE(7,990)YNRATE,YXRATE,DYRATE

	TOST1=0
	TOST2=0
	TOST3=0
	TOST4=0
	TAVS=0
	TRPM=0
1000	CONTINUE
	IF(J.LT.JRATE)THEN
	HPR1=HD(IPR1)*REFH+DATUM-(ZPR1+PDIA(NPR1))	
	HPR2=HD(IPR2)*REFH+DATUM-(ZPR2+PDIA(NPR2))
	HPR3=HD(IPR3)*REFH+DATUM-(ZPR3+PDIA(NPR3))
	TIME=T*REFT	
	WRITE(7,1010)TIME,HPR1,HPR2,HPR3
1010	FORMAT(7F10.3)
	ENDIF

	IF(J.LT.JSTAV)THEN
	TIME=T*REFT
	IF(NOSTD.GT.0)THEN
	DO I=1,NOSTD
	VCOSTD(I)=VOST(I)*REFV*(PDIA(NPREF)/DCOST(I))**2
	IF(I.EQ.1)THEN
	WRITE(11,1010)TIME,VCOSTD(I)
	TOST1=TOST1+1
	ELSEIF(I.EQ.2)THEN
	WRITE(12,1010)TIME,VCOSTD(I)
	TOST2=TOST2+1
	ELSEIF(I.EQ.3)THEN
	WRITE(13,1010)TIME,VCOSTD(I)
	TOST3=TOST3+1
	ELSEIF(I.EQ.4)THEN
	WRITE(14,1010)TIME,VCOSTD(I)
	TOST4=TOST4+1
	ENDIF
	END DO
	ENDIF
	IF(CODEAC.EQ.'YES')THEN
	VCPAC=VAC*REFV*(PDIA(NPREF)/DCAC)**2	
	IF(KACTYP.EQ.2.AND.VAC.LT.0)THEN
	VCPAC=VAC*REFV*(PDIA(NPREF)/DBYAC)**2
	ENDIF
	WRITE(15,1010)TIME,VCPAC
	TAVS=TAVS+1
	ENDIF
	ENDIF

	IF(ICV(ISPLOT).EQ.0)THEN
	NRPM=IPMPSP(ISPLOT)*AL(ISPLOT)
	WRITE(10,1015)TIME,NRPM
1015	FORMAT(F10.3,I10)
	TRPM=TRPM+1
	ENDIF

        IF(J.LE.4.OR.J.GE.(JMAX-5).OR.JPCR.EQ.JPC)THEN
        IF(JPCR.EQ.JPC)JPCR=0                               
        WRITE (6,1020) T
1020     FORMAT(//2X,'T=',F7.3/)
	IF(NPMP.GT.0)THEN
	WRITE (6,1025)
1025	FORMAT (2X,'IP',2X,'KI',4X,'AL',6X,'BET',6X,'VPU',
     *          6X,'HPU',6X,'TAU',6X,'KDNRR'/)
	DO IP=1,NPMP
	WRITE (6,1030)IP,KI(IP),AL(IP),BET(IP),VPU(IP),HPU(IP),TA(IP),
     *  KDNRR(IP)
1030	FORMAT (2X,I2,2X,I2,2X,F7.4,2X,F7.4,2X,F7.4,2X,F7.4,2X,F7.4,5X,
     *  I2)
	END DO
	ENDIF
	IF(NBST.GT.0)THEN
	WRITE (6,1035)
1035	FORMAT(/2X,'IB',2X,'KI',4X,'ALB',5X,'BETB',5X,'VPUB',
     *          5X,'HPUB',5X,'TAUB',5X,'KDNRR'/)
	DO IB=1,NBST
	WRITE (6,1030)IB,KI(IB),ALB(IB),BETB(IB),VPUB(IB),HPUB(IB),
     *  TAB(IB),KDNRRB(IB)
	END DO
	ENDIF
	IF(CODEAC.EQ.'YES')THEN
        WRITE (6,1050)ITERAC,C,HAC,VAC
1050     FORMAT(/2X,'ITERAC=',I3,2X,'C=',F8.5,2X, 'HAC=',F8.5,
     *  2X,'VAC=',F8.5)
	ENDIF
        IF(NZV.GT.0)WRITE (6,1080)(TAZV(I),I=1,NZV)
1080    FORMAT(/,2X,'TAUZV',10F10.5/)
	IF(NOST.GT.0)THEN
	WRITE (6,1090)
1090	FORMAT(2X,'NPOST',2X,'IOST',5X,'HOST',5X,'VOST'/)
	DO I=1,NOST
	WRITE(6,1100)NPOST(I),IOST(I),HOST(I),VOST(I)
1100	FORMAT (2X,I3,3X,I4,2X,F8.5,2X,F8.5)
	END DO
	ENDIF
	IF(NSS.GT.0)THEN
	WRITE (6,1110)(HSS(I),I=1,NSS)
1110	FORMAT(/2X,'HSS',5F10.5/)
	ENDIF
	IF(NSV.GT.0)THEN
        WRITE(6,1120)
1120	FORMAT(/2X,'NO.',2X,'KSV',4X,'OSV',6X,'VSV'/)
	DO I=1,NSV
	WRITE(6,1130)I,KSV(I),OSV(I),VSV(I)
1130	FORMAT(2X,I3,2X,I3,2X,F7.5,2X,F7.5)
	END DO
	ENDIF

        WRITE (6,1140)
1140     FORMAT(/4X,'NP',4X,'IN',6X,'HU',10X,'HD',10X,'VU',10X,'VD'/)
	DO II=1,NPRNT
	NP=NPPRNT(II)
	IN=IPRNT(II)
	NN=NNPRNT(II)
        WRITE (6,1150)NP,IN,HU(NN),HD(NN),VU(NN),VD(NN)
1150    FORMAT(2X,I3,3X,I3,4F12.5)
	END DO
        IF(J.EQ.4)JPCR=0
	ENDIF 

        J=J+1
        T=T+DT
        IF(J.GT.JMAX)GO TO 2000
	IF(CODECS.EQ.'YES')THEN
	DO II=1,NCS
        KCOLSP(II)=0
	END DO
	ENDIF

	DO 1400 IGN=1,NNMAX

	IF(TYPE(IGN).EQ.'ORD')THEN
	 NPL=IUSP(IGN,1)
	 NPR=IDSP(IGN,1)
	 NNL=NSTART(NPL)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TYPE(IGN).EQ.'PMP')THEN
	 IP=IPMP(IGN)	 
	 NPR=IDSP(IGN,1)
	 NNR=NSTART(NPR)+1
         CM=HU(NNR)-VU(NNR)*(B(NPR)-R(NPR)*ABS(VU(NNR)))	 
	 IF (ITRIP(IP).EQ.1) THEN
	  IF (ICV(IP).EQ.0) THEN
	  IF(KODPHV(IP).EQ.4)THEN
	  FK=((T/TC(IP))*40.0)+1.0
	  K=((T/TC(IP))*40)+1
	  TA(IP)=(FK-K)*TAU(K+1)+(K+1-FK)*TAU(K)
	  IF((TA(IP).LT.0.001).OR.(T.GE.TC(IP)))ICV(IP)=1
	  ELSEIF(KODPHV(IP).EQ.5)THEN
	  IF(T.LT.TC1(IP))THEN
	  FK=((T/TC1(IP))*36.0)+1.0
	  K=((T/TC1(IP))*36)+1
	  TA(IP)=(FK-K)*TAU(K+1)+(K+1-FK)*TAU(K)
	  ELSE
	  TT=T-TC1(IP)
	  FK=((TT/TC2(IP))*4.0)+37.0
	  K=((TT/TC2(IP))*4)+37	
          TA(IP)=(FK-K)*TAU(K+1)+(K+1-FK)*TAU(K)
	  IF((TA(IP).LT.0.001).OR.(T.GE.(TC1(IP)+TC2(IP))))ICV(IP)=1
	  ENDIF
	  ELSEIF (KODPHV(IP).EQ.2) THEN
	  IF(T.LT.(TVO(IP)+DLY(IP)))THEN
	  TA(IP)=1.0
	  ELSE
	  TT=T-(TVO(IP)+DLY(IP))
	  FK=((TT/TC(IP))*40.0)+1.0
	  K=((TT/TC(IP))*40)+1
	  TA(IP)=(FK-K)*TAU(K+1)+(K+1-FK)*TAU(K)
	  IF((TA(IP).LT.0.001).OR.(T.GE.(TVO(IP)+DLY(IP)+TC(IP))))
     *    ICV(IP)=1
	  ENDIF
	  ELSEIF(KODPHV(IP).EQ.3)THEN
	  IF(T.LT.(TVO(IP)+DLY(IP)))THEN
	  TA(IP)=1.0
	  ELSEIF(T.LT.(TVO(IP)+DLY(IP)+TC1(IP)))THEN
	  TT=T-(TVO(IP)+DLY(IP))
	  FK=((TT/TC1(IP))*36.0)+1.0
	  K=((TT/TC1(IP))*36)+1
	  TA(IP)=(FK-K)*TAU(K+1)+(K+1-FK)*TAU(K)
	  ELSE
	  TT=T-(TVO(IP)+DLY(IP)+TC1(IP))
	  FK=((TT/TC2(IP))*4.0)+37.0
	  K=((TT/TC2(IP))*4)+37	
          TA(IP)=(FK-K)*TAU(K+1)+(K+1-FK)*TAU(K)
	  IF((TA(IP).LT.0.001).OR.(T.GE.(TVO(IP)+DLY(IP)+
     *    TC1(IP)+TC2(IP))))ICV(IP)=1
	  ENDIF
	  ELSEIF(KODPHV(IP).EQ.1)THEN
	  TA(IP)=1.0
	  IF(T.GT.(TVO(IP)+DLY(IP)))ICV(IP)=1
	  ELSEIF(KODPHV(IP).EQ.6)THEN
	  IF(VPUO(IP).GT.0.0)THEN
	  VREL=VPUO(IP)/VPUI(IP)
          IVEL=VREL/0.05+1
          PR=(VREL-(IVEL-1)*0.05)/0.05
          OPG=OGDP(IVEL+1)*PR+OGDP(IVEL)*(1.0-PR)
          IOG=OPG/0.05+1
          PR=(OPG-(IOG-1)*0.05)/0.05
          TA(IP)=TAUDP(IOG+1)*PR+TAUDP(IOG)*(1.0-PR)
	  IF(TA(IP).LT.0.001)THEN
          ICV(IP)=1
	  TVO(IP)=T
	  ENDIF
	  ELSE
	  ICV(IP)=1
	  ENDIF
	  ENDIF
	  ENDIF

	  IF(ICV(IP).EQ.0)THEN
          DHOO=DHO(IP)/(TA(IP)*TA(IP))
	  CALL FPUMP (KODPHV(IP),IP,T,ZPU(IP),ALO(IP),VPUO(IP),
     *    BETO(IP),CI(IP),HRAT(IP),VI(NPR),VPUI(IP),AL(IP),VPU(IP),
     *    BET(IP),WHP,WBP,DHOO,CM,B(NPR),DLY(IP),TVO(IP),HNUGN,HNDGN,
     *    VNUGN,VNDGN,KI(IP),HPU(IP),KDNRR(IP),FKNRR(IP),CODNRR(IP))
	  ELSE
	  HNDGN=CM
	  VNDGN=0.0
	  HNUGN=ZPU(IP)
	  VNUGN=0.0
	  ENDIF
	 ELSE
	  AL(IP)=1.0
	  CALL RPUMP (IP,T,ZPU(IP),HRAT(IP),VI(NPR),VPUI(IP),AL(IP),
     *    VPU(IP),WHP,DHO(IP),CM,B(NPR),HNUGN,HNDGN,VNUGN,VNDGN,
     *    KI(IP),HPU(IP))
	 ENDIF
	 ALO(IP)=AL(IP)
	 BETO(IP)=BET(IP)
	 VPUO(IP)=VPU(IP)
	 NN=NSTART(NPR)
	 HNU(NN)=HNUGN
	 HND(NN)=HNDGN
	 VNU(NN)=VNUGN
	 VND(NN)=VNDGN	 

	ELSEIF(TYPE(IGN).EQ.'BST')THEN
	 IB=IBST(IGN)
	 NPL=IUSP(IGN,1)
	 NPR=IDSP(IGN,1)
	 NNL=NSTART(NPL)+NGAP(NPL)-1
	 NNR=NSTART(NPR)+1
	 CP=HD(NNL)+VD(NNL)*(B(NPL)-R(NPL)*ABS(VD(NNL)))
         CM=HU(NNR)-VU(NNR)*(B(NPR)-R(NPR)*ABS(VU(NNR)))	 
	 IF (ITRIPB(IB).EQ.1) THEN
	  IF (ICVB(IB).EQ.0) THEN
	  IF(KODBSV(IB).EQ.4)THEN
	  FK=((T/TCB(IB))*40.0)+1.0
	  K=((T/TCB(IB))*40)+1
	  TAB(IB)=(FK-K)*TAU(K+1)+(K+1-FK)*TAU(K)
	  IF((TAB(IB).LT.0.001).OR.(T.GE.TCB(IB)))ICVB(IB)=1
	  ELSEIF(KODBSV(IB).EQ.5)THEN
	  IF(T.LT.TC1B(IB))THEN
	  FK=((T/TC1B(IB))*36.0)+1.0
	  K=((T/TC1B(IB))*36)+1
	  TAB(IB)=(FK-K)*TAU(K+1)+(K+1-FK)*TAU(K)
	  ELSE
	  TT=T-TC1B(IB)
	  FK=((TT/TC2B(IB))*4.0)+37.0
	  K=((TT/TC2B(IB))*4)+37	
          TAB(IB)=(FK-K)*TAU(K+1)+(K+1-FK)*TAU(K)
	  IF((TAB(IB).LT.0.001).OR.(T.GE.(TC1B(IB)+TC2B(IB))))
     *    ICVB(IB)=1
	  ENDIF
	  ELSEIF (KODBSV(IB).EQ.2) THEN
	  IF(T.LT.(TVOB(IB)+DLYB(IB)))THEN
	  TAB(IB)=1.0
	  ELSE
	  TT=T-(TVOB(IB)+DLYB(IB))
	  FK=((TT/TCB(IB))*40.0)+1.0
	  K=((TT/TCB(IB))*40)+1
	  TAB(IB)=(FK-K)*TAU(K+1)+(K+1-FK)*TAU(K)
	  IF((TAB(IB).LT.0.001).OR.(T.GE.(TVOB(IB)+DLYB(IB)+TCB(IB))))
     *    ICVB(IB)=1
	  ENDIF
	  ELSEIF(KODBSV(IB).EQ.3)THEN
	  IF(T.LT.(TVOB(IB)+DLYB(IB)))THEN
	  TAB(IB)=1.0
	  ELSEIF(T.LT.(TVOB(IB)+DLYB(IB)+TC1B(IB)))THEN
	  TT=T-(TVOB(IB)+DLYB(IB))
	  FK=((TT/TC1B(IB))*36.0)+1.0
	  K=((TT/TC1B(IB))*36)+1
	  TAB(IB)=(FK-K)*TAU(K+1)+(K+1-FK)*TAU(K)
	  ELSE
	  TT=T-(TVOB(IB)+DLYB(IB)+TC1B(IB))
	  FK=((TT/TC2B(IB))*4.0)+37.0
	  K=((TT/TC2B(IB))*4)+37	
          TAB(IB)=(FK-K)*TAU(K+1)+(K+1-FK)*TAU(K)
	  IF((TAB(IB).LT.0.001).OR.(T.GE.(TVOB(IB)+DLYB(IB)+
     *    TC1B(IB)+TC2B(IB))))ICVB(IB)=1
	  ENDIF
	  ELSEIF(KODBSV(IB).EQ.1)THEN
	  TAB(IB)=1.0
	  IF(T.GT.(TVOB(IB)+DLYB(IB)))ICVB(IB)=1
	  ELSEIF(KODBSV(IP).EQ.6)THEN
	  IF(VPUOB(IB).GT.0.0)THEN
	  VREL=VPUOB(IB)/VPUIB(IB)
          IVEL=VREL/0.05+1
          PR=(VREL-(IVEL-1)*0.05)/0.05
          OPG=OGDP(IVEL+1)*PR+OGDP(IVEL)*(1.0-PR)
          IOG=OPG/0.05+1
          PR=(OPG-(IOG-1)*0.05)/0.05
          TAB(IB)=TAUDP(IOG+1)*PR+TAUDP(IOG)*(1.0-PR)
	  IF(TAB(IB).LT.0.001)THEN
          ICVB(IB)=1
	  TVOB(IB)=T
	  ENDIF
	  ELSE
	  ICVB(IB)=1
	  ENDIF
	  ENDIF
	  ENDIF

	  IF(ICVB(IB).EQ.0)THEN
          DHOO=DHOB(IB)/(TAB(IB)*TAB(IB))
	  CALL FBST(KODBSV(IB),IB,T,ALOB(IB),VPUOB(IB),BETOB(IB),
     *    CIB(IB),HRATB(IB),VI(NPR),VI(NPL),VPUIB(IB),AL(IB),
     *    VPUB(IB),BETB(IB),WHB,WBB,DHOO,CM,CP,B(NPR),B(NPL),
     *    DLYB(IB),TVOB(IB),HNUGN,HNDGN,VNUGN,VNDGN,KIB,HPUB(IB),
     *    KDNRRB(IB),FKNRRB(IB),CODNRB(IB))
	  ELSE
	  HNDGN=CM
	  VNDGN=0.0
	  HNUGN=CP
	  VNUGN=0.0
	  ENDIF
	 ELSE
	  ALB(IP)=1.0
	  CALL RBST(IB,T,HRATB(IB),VI(NPL),VI(NPR),VPUIB(IB),ALB(IB),
     *    VPUB(IB),WHB(IB,1),DHOB(IB),CM,CP,B(NPR),B(NPL),HNUGN,HNDGN,
     *    VNUGN,VNDGN,KIB(IB),HPUB(IB))
	 ENDIF
	 ALOB(IB)=ALB(IB)
	 BETOB(IB)=BETB(IB)
	 VPUOB(IB)=VPUB(IB)
	 NN=NSTART(NPL)+NGAP(NPL)
	 HNU(NN)=HNUGN
	 VNU(NN)=VNUGN
	 HND(NN)=HNU(NN)
	 VND(NN)=VNU(NN)
	 NN=NSTART(NPR)
	 HND(NN)=HNDGN
	 VND(NN)=VNDGN
	 HNU(NN)=HND(NN)
	 VNU(NN)=VND(NN)
	
	ELSEIF(TYPE(IGN).EQ.'RES')THEN
	 NPL=IUSP(IGN,1)
	 NNL=NSTART(NPL)+NGAP(NPL)-1
	 CP=HD(NNL)+VD(NNL)*(B(NPL)-R(NPL)*ABS(VD(NNL)))
	 JJ=IRES(IGN)
         IF(KODEDS.EQ.0)THEN
          HNGN=HGLR(JJ)
          VNGN=(CP-HNGN)/B(NPL)
	 ELSE
	  IF(T.LT.(DLYDS+TOPDS))THEN
 	  TT=T-DLYDS
	  IF(TT.GT.0.0)THEN
	  FK=1.0+(TT/TOPDS)*40
	  K=FK
	  TADS=(FK-K)*TAU(K+1)+(K+1-FK)*TAU(K)
	  ELSE
	  TADS=1.0
	  ENDIF
	  AA=HLV/(TADS*TADS)
	  BB=B(NPL)
	  CC=HGLR(JJ)-CP
	  IF(CC.LT.0)THEN
	  VNGN=(-BB+SQRT(BB*BB-4.0*AA*CC))/(2.0*AA)
	  ELSE
    	  VNGN=(BB-SQRT(BB*BB+4.0*AA*CC))/(2.0*AA)
	  ENDIF
	  HNGN=CP-B(NPL)*VNGN
	  QEXIT=QEXIT+DT*VNGN*AR(NPL)
	  ELSE
	  VNGN=0.0
          HNGN=CP
	  ENDIF
	 ENDIF
	 NN=NSTART(NPL)+NGAP(NPL)
	 HNU(NN)=HNGN
	 VNU(NN)=VNGN
	 HND(NN)=HGLR(JJ)
	 VND(NN)=VNU(NN)

	ELSEIF(TYPE(IGN).EQ.'SOU')THEN
	 NPR=IDSP(IGN,1)
	 NNR=NSTART(NPR)+1
         CM=HU(NNR)-VU(NNR)*(B(NPR)-R(NPR)*ABS(VU(NNR)))	 
	 JJ=ISOU(IGN)
	 HNGN=HSOU(JJ)
	 VNGN=(HNGN-CM)/B(NPR)
	 NN=NSTART(NPR)
	 HND(NN)=HNGN
	 VND(NN)=VNGN
	 HNU(NN)=HSOU(JJ)
	 VNU(NN)=VND(NN)

	ENDIF

1400	CONTINUE
	

	IPTOST=1
	IPTZV=1
	IPTNRV=1
	IPTAV=1
	IPTACV=1
	IPTSS=1
	IPTSV=1
	IPTCS=1
	NOSTC=IOROST(1)
        NZVC=IORZV(1)
        NNRVC=IORNRV(1)
        NAVC=IORAV(1)
	NACVC=IORACV(1)
	NSSC=IORSS(1)
	NSVC=IORSV(1)
	NCSC=IORCS(1)

        DO 1600 IIP=1,NPMAX
	IF(NGAP(IIP).LT.2)GO TO 1600
	DO 1500 II=2,NGAP(IIP)
	NNL=NSTART(IIP)+II-2
	NNR=NSTART(IIP)+II 
	NN=NSTART(IIP)+II-1 
        CP=HD(NNL)+VD(NNL)*(B(IIP)-R(IIP)*ABS(VD(NNL)))
        CM=HU(NNR)-VU(NNR)*(B(IIP)-R(IIP)*ABS(VU(NNR)))
	ICY=0
 
        IF (ICY.EQ.0.AND.NPAC.EQ.IIP.AND.IAC.EQ.II)THEN
        DVAC=VAC-VACO
        VACO=VAC
        FVAC=1.0
        IF(DLVAC.NE.0.0)THEN
        IF(J.GT.2)FVAC=DVAC/DLVAC          
        IF(FVAC.LT.0.0)FVAC=0.0
        IF(FVAC.GT.10.0)FVAC=10.0
	ENDIF
        DLVAC=DVAC
        VAC=VAC+DVAC*FVAC

        ITERAC=1
1160    C=CO+(VAC+VACO)/ACK
        DHOR=DHOR1
        IF(VAC.LT.0.0)DHOR=DHOR2
        IF(T.GE.(TVOAC+DLYAC))GO TO 1190                   
        FAC=(CP+CM+B(IIP)*VAC)/2.0+DHOR*VAC*ABS(VAC)-HACIC*C
     *  **(-P)+HATC
        FACV=B(IIP)/2.0+2.0*DHOR*ABS(VAC)+HACIC*P*C**(-P1)/ACK
        DVAC=-FAC/FACV
        VAC=VAC+DVAC
        IF(ABS(DVAC).GE.TOLAC)THEN                 
        ITERAC=ITERAC+1
        IF(ITERAC.LE.50)GO TO 1160
        WRITE (6,1170) T,C,DHOR,VAC,FAC,FACV,DVAC,TVOAC
1170     FORMAT(//2X,'AIR CHAMBER B.C TROUBLE',15X,8E14.4)
        GO TO 3000
	ENDIF
 
        C=CO+(VAC+VACO)/ACK
        HAC=HACIC*C**(-P)-HATC
        HND(NN)=HAC-DHOR*VAC*ABS(VAC)
        VND(NN)=(HND(NN)-CM)/B(IIP)
        VNU(NN)=VND(NN)-VAC
        HNU(NN)=HND(NN)
        IF((VNU(NN).LT.0.0).AND.(TVOAC.GT.9999.0))THEN
        TVOAC=T
        IF(DLYAC.LT.0.001)GO TO 1190
	ELSE
	GO TO 1220
	ENDIF
 
1190    ITERAC=1
1200    C=CO+(VAC+VACO)/ACK
        DHOR=DHOR1
        IF(VAC.LT.0.0)DHOR=DHOR2
        FAC=CM+B(IIP)*VAC+DHOR*VAC*ABS(VAC)-HACIC*C**(-P)+HATC
        FACV=B(IIP)+2.0*DHOR*ABS(VAC)+HACIC*P*C**(-P1)/ACK
        DVAC=-FAC/FACV
        VAC=VAC+DVAC
        IF(ABS(DVAC).GE.TOLAC)THEN
        ITERAC=ITERAC+1
        IF(ITERAC.LE.50)GO TO 1200
        WRITE (6,1210) T,C,DHOR,VAC,FAC,FACV,DVAC,TVOAC
1210     FORMAT(//2X,'AIR CHAMBER B.C TROUBLE',15X,8E14.4)
        GO TO 3000
	ENDIF 

        C=CO+(VAC+VACO)/ACK
        HAC=HACIC*C**(-P)-HATC
        HND(NN)=HAC-DHOR*VAC*ABS(VAC)
        VND(NN)=VAC
        VNU(NN)=0.0
        HNU(NN)=CP
1220    ICY=1
	END IF

 
        IF (ICY.EQ.0.AND.NPAIRV(NAVC).EQ.IIP.AND.IAV(NAVC).EQ.II)THEN 
        HNU(NN)=0.5*(CP+CM)
        IF(HNU(NN).LT.ZAV(NAVC))THEN                                   
        COF=-4.0*(CP+CM)+B(IIP)*B(IIP)*AVC(NAVC)*AVC(NAVC)
        AVFN=COF*COF-16.0*((CP+CM)*(CP+CM)-B(IIP)*B(IIP)*
     *  AVC(NAVC)*AVC(NAVC)*ZAV(NAVC))
        HNU(NN)=-COF/8.0+SQRT(AVFN)/8.0
        HND(NN)=HNU(NN)
        VNU(NN)=(CP-HNU(NN))/B(IIP)
        VND(NN)=(HNU(NN)-CM)/B(IIP)
	ELSE
        HND(NN)=HNU(NN)
        VNU(NN)=(CP-HNU(NN))/B(IIP)
        VND(NN)=VNU(NN)
	ENDIF
	IPTAV=IPTAV+1
        NAVC=IORAV(IPTAV)
	ICY=1
	END IF
        
 
        IF (ICY.EQ.0.AND.NPACV(NACVC).EQ.IIP.AND.IACV(NACVC).EQ.II)THEN 
        HNU(NN)=0.5*(CP+CM)
        IF(HNU(NN).LT.ZACV(NACVC))THEN                                   
        COF=-4.0*(CP+CM)+B(IIP)*B(IIP)*ACVC(NACVC)*ACVC(NACVC)
        AVFN=COF*COF-16.0*((CP+CM)*(CP+CM)-B(IIP)*B(IIP)*
     *  ACVC(NACVC)*ACVC(NACVC)*ZACV(NACVC))
        HNU(NN)=-COF/8.0+SQRT(AVFN)/8.0
        HND(NN)=HNU(NN)
        VNU(NN)=(CP-HNU(NN))/B(IIP)
        VND(NN)=(HNU(NN)-CM)/B(IIP)
	ELSE
        HND(NN)=HNU(NN)
        VNU(NN)=(CP-HNU(NN))/B(IIP)
        VND(NN)=VNU(NN)
	ENDIF
	IPTACV=IPTACV+1 
        NACVC=IORACV(IPTACV)
	ICY=1
	END IF


        IF (ICY.EQ.0.AND.NPCS(NCSC).EQ.IIP.AND.ICS(NCSC).EQ.II)THEN
	IF(ICAV(NCSC).EQ.0)THEN
        HNU(NN)=0.5*(CP+CM)
        VNU(NN)=(CP-HNU(NN))/B(IIP)
        HND(NN)=HNU(NN)
        VND(NN)=VNU(NN)
        IF(HNU(NN).LT.ZCS(NCSC).AND.KCOLSP(NCSC).EQ.0)THEN
	ICAV(NCSC)=1
        HNU(NN)=ZCS(NCSC)
        HND(NN)=ZCS(NCSC)
        VNU(NN)=(CP-HNU(NN))/B(IIP)
        VND(NN)=(HNU(NN)-CM)/B(IIP)
        VCAV(NCSC)=VCAV(NCSC)+0.5*DT*(VD(NN)-VU(NN)+VND(NN)-VNU(NN))	
	ENDIF
	ELSE
        HNU(NN)=ZCS(NCSC) 
        HND(NN)=ZCS(NCSC)
        VNU(NN)=(CP-HNU(NN))/B(IIP)
        VND(NN)=(HNU(NN)-CM)/B(IIP)
        VCAV(NCSC)=VCAV(NCSC)+0.5*DT*(VD(NN)-VU(NN)+VND(NN)-VNU(NN))
        IF(VCAV(NCSC).LE.0.0)THEN
        ICAV(NCSC)=0
        VCAV(NCSC)=0.0
        KCOLSP(NCSC)=1
        HNU(NN)=0.5*(CP+CM)
        VNU(NN)=(CP-HNU(NN))/B(IIP)
        HND(NN)=HNU(NN)
        VND(NN)=VNU(NN)
	ENDIF
	ENDIF
	IPTCS=IPTCS+1
	NCSC=IORCS(IPTCS)
	ICY=1
	END IF

 	 
        IF (ICY.EQ.0.AND.NPINRV(NNRVC).EQ.IIP.AND.INRV(NNRVC).EQ.II)THEN
        CMP=CM-CP
        KNRV=0
        IF(T.GT.(TVONRV(NNRVC)+DLYNRV(NNRVC)).AND.CMP.GT.0.0)KNRV=1
        IF(KNRV.EQ.1)THEN
        IF(DHBNRV(NNRVC).GT.999.0)THEN
        VNU(NN)=0.0
        VND(NN)=0.0
        HNU(NN)=CP
        HND(NN)=CM
	ELSE
        VNU(NN)=(B(IIP)-SQRT(B(IIP)*B(IIP)+DHBNRV(NNRVC)*CMP))/
     *  DHBNRV(NNRVC)
        VND(NN)=VNU(NN)
        HNU(NN)=CP-B(IIP)*VNU(NN)
        HND(NN)=CM+B(IIP)*VND(NN)
	ENDIF
	ELSE
        VNU(NN)=-CMP/(2.0*B(IIP))
        VND(NN)=VNU(NN)
        HNU(NN)=CP-B(IIP)*VNU(NN)
        HND(NN)=CM+B(IIP)*VND(NN)
        IF(VU(NN).GT.0.0.AND.VNU(NN).LT.0.0.AND.
     *  TVONRV(NNRVC).GT.9999)TVONRV(NNRVC)=T
	ENDIF
	IPTNRV=IPTNRV+1
        NNRVC=IORNRV(IPTNRV)
	ICY=1
	END IF


        IF (ICY.EQ.0.AND.NPOST(NOSTC).EQ.IIP.AND.IOST(NOSTC).EQ.II)THEN
        IF(HU(NN).GT.HTOST(NOSTC))THEN
        HNU(NN)=0.5*(CP+CM)
        HND(NN)=HNU(NN)
        VNU(NN)=(CP-HNU(NN))/B(IIP)
        VND(NN)=VNU(NN)
	IF(HNU(NN).LT.HTOST(NOSTC))THEN           
	IF(TOSTS(NOSTC).GT.999)TOSTS(NOSTC)=T
        BB=(1.0+OSTK(NOSTC)*B(IIP))/(2.0*OSTK(NOSTC)*HLOST(NOSTC))
        CC=(VD(NN)-VU(NN)-2.0*OSTK(NOSTC)*HOSTO(NOSTC)+
     *  OSTK(NOSTC)*(CP+CM))/(2.0*OSTK(NOSTC)*HLOST(NOSTC))
        VOST(NOSTC)=(-BB+SQRT(BB*BB-4.0*CC))/2.0
        HOST(NOSTC)=HOST(NOSTC)+(VU(NN)-VD(NN)-VOST(NOSTC))/
     *  (2.0*OSTK(NOSTC))
        HNU(NN)=HOST(NOSTC)-HLOST(NOSTC)*VOST(NOSTC)*VOST(NOSTC)
        HND(NN)=HNU(NN)
        VNU(NN)=(CP-HNU(NN))/B(IIP)
        VND(NN)=VNU(NN)+VOST(NOSTC)
	ENDIF
	ELSE
        BB=(1.0+OSTK(NOSTC)*B(IIP))/(2.0*OSTK(NOSTC)*HLOST(NOSTC))
        CC=(VD(NN)-VU(NN)-2.0*OSTK(NOSTC)*HOSTO(NOSTC)+
     *  OSTK(NOSTC)*(CP+CM))/(2.0*OSTK(NOSTC)*HLOST(NOSTC))
	IF(CC.LT.0.0)THEN
        VOST(NOSTC)=(-BB+SQRT(BB*BB-4.0*CC))/2.0
        HOST(NOSTC)=HOST(NOSTC)+(VU(NN)-VD(NN)-VOST(NOSTC))/
     *  (2.0*OSTK(NOSTC))
        HNU(NN)=HOST(NOSTC)-HLOST(NOSTC)*VOST(NOSTC)*VOST(NOSTC)
        HND(NN)=HNU(NN)
        VNU(NN)=(CP-HNU(NN))/B(IIP)
        VND(NN)=VNU(NN)+VOST(NOSTC)
	ELSE
	IF(TOSTE(NOSTC).GT.999)TOSTE(NOSTC)=T
        HNU(NN)=0.5*(CP+CM)
        HND(NN)=HNU(NN)
        VNU(NN)=(CP-HNU(NN))/B(IIP)
        VND(NN)=VNU(NN)
	VOST(NOSTC)=0.0 
	ENDIF
	ENDIF
	IF(HOST(NOSTC).LE.OSTBED(NOSTC).AND.TDOST(NOSTC).GT.999)
     *	TDOST(NOSTC)=T
	IPTOST=IPTOST+1
        NOSTC=IOROST(IPTOST)
	ICY=1
	END IF


        IF (ICY.EQ.0.AND.NPZVV(NZVC).EQ.IIP.AND.IZV(NZVC).EQ.II)THEN
	IF(DHB(NZVC).LT.999)THEN
         IF(VU(NN).GT.0.0)THEN
  	 VREL=VU(NN)/VI(IIP)
         IVEL=VREL/0.05+1
         PR=(VREL-(IVEL-1)*0.05)/0.05
         OPG=OG(IVEL+1)*PR+OG(IVEL)*(1.0-PR)
         IOG=OPG/0.05+1
         PR=(OPG-(IOG-1)*0.05)/0.05
         TAZV(NZVC)=TAUZV(IOG+1)*PR+TAUZV(IOG)*(1.0-PR)
         IF(TAZV(NZVC).LT.DHZB(NZVC))TAZV(NZVC)=DHZB(NZVC)
         ELSE
         TAZV(NZVC)=DHZB(NZVC)
	 ENDIF
         CPM=CP-CM
         BB=2.0*B(IIP)*TAZV(NZVC)*TAZV(NZVC)
         CC=TAZV(NZVC)*TAZV(NZVC)*CPM
         IF(CPM.GE.0.)THEN
         VNU(NN)=(SQRT(BB*BB+4.0*DHZ(NZVC)*CC)-BB)/(2.0*DHZ(NZVC))
	 ELSE
         VNU(NN)=(BB-SQRT(BB*BB-4.0*DHZ(NZVC)*CC))/(2.0*DHZ(NZVC))
	 ENDIF
         VND(NN)=VNU(NN)
         HNU(NN)=CP-B(IIP)*VNU(NN)
         HND(NN)=CM+B(IIP)*VND(NN)
	ELSE
         IF(VU(NN).GT.0.0)THEN
  	 VREL=VU(NN)/VI(IIP)
         IVEL=VREL/0.05+1
         PR=(VREL-(IVEL-1)*0.05)/0.05
         OPG=OG(IVEL+1)*PR+OG(IVEL)*(1.0-PR)
         IOG=OPG/0.05+1
         PR=(OPG-(IOG-1)*0.05)/0.05
         TAZV(NZVC)=TAUZV(IOG+1)*PR+TAUZV(IOG)*(1.0-PR)
	 IF(TAZV(NZVC).LT.0.001)TAZV(NZVC)=0.0
	 ELSE
	 TAZV(NZVC)=0.0
	 ENDIF
	 IF(TAZV(NZVC).GE.0.001)THEN
         CPM=CP-CM
         BB=2.0*B(IIP)*TAZV(NZVC)*TAZV(NZVC)
         CC=TAZV(NZVC)*TAZV(NZVC)*CPM
         IF(CPM.GE.0.)THEN
         VNU(NN)=(SQRT(BB*BB+4.0*DHZ(NZVC)*CC)-BB)/(2.0*DHZ(NZVC))
	 ELSE
         VNU(NN)=(BB-SQRT(BB*BB-4.0*DHZ(NZVC)*CC))/(2.0*DHZ(NZVC))
	 ENDIF
         VND(NN)=VNU(NN)
         HNU(NN)=CP-B(IIP)*VNU(NN)
         HND(NN)=CM+B(IIP)*VND(NN)
	 ELSE
         VNU(NN)=0.0
         VND(NN)=0.0
         HNU(NN)=CP
         HND(NN)=CM	
	 ENDIF
	ENDIF
	IPTZV=IPTZV+1
        NZVC=IORZV(IPTZV)
	ICY=1
	END IF


	IF(ICY.EQ.0.AND.NPSV(NSVC).EQ.IIP.AND.ISV(NSVC).EQ.II)THEN
	IF(KSV(NSVC).EQ.0)THEN
	  OSV(NSVC)=0.0
	  CALL SVALVE(B(IIP),AR(IIP),CP,CM,KSV(NSVC),OSV(NSVC),
     *	  ZSV(NSVC),CSV(NSVC),VSV(NSVC),HNU(NN),HND(NN),VNU(NN),
     *    VND(NN),AIRC(NSVC))
    	  IF(HNU(NN).LT.HMNSV(NSVC).OR.HNU(NN).GT.
     *    HMXSV(NSVC))THEN
	  KSV(NSVC)=2
	  OSV(NSVC)=1.0
	  ISVST(NSVC)=ISVST(NSVC)+1
	  KSVST(NSVC,ISVST(NSVC))='0 TO 2'
	  TSVST(NSVC,ISVST(NSVC))=T
	  IF(HNU(NN).LT.HMNSV(NSVC))THEN
	  IPILOT(NSVC,ISVST(NSVC))=1
	  ELSE
	  IPILOT(NSVC,ISVST(NSVC))=2
	  ENDIF
	  WRITE(4,1250)NSVC,T,HNU(NN)
1250	  FORMAT(2X,'*** SURGE VALVE STATUS CHANGE FROM CLOSED TO ' 
     *    'OPEN',2X,'NSVC=',I3,2X,'TIME=',F8.3,2X,'HNU=',F6.3)
	  CALL SVALVE(B(IIP),AR(IIP),CP,CM,KSV(NSVC),
     *    OSV(NSVC),ZSV(NSVC),CSV(NSVC),VSV(NSVC),
     *	  HNU(NN),HND(NN),VNU(NN),VND(NN),AIRC(NSVC))
	  END IF
	  IF(VSV(NSVC).GT.0.0)QSV(NSVC)=QSV(NSVC)+VSV(NSVC)*DT 
	ELSE IF (KSV(NSVC).EQ.1)THEN
	  OSV(NSVC)=1.0-(T-TSV(NSVC))/TCSV(NSVC)
	  IF(OSV(NSVC).GT.1.0)OSV(NSVC)=1.0
	  IF(OSV(NSVC).LT.0.0001)THEN
	  KSV(NSVC)=0
	  OSV(NSVC)=0.0
	  END IF
          CALL SVALVE(B(IIP),AR(IIP),CP,CM,KSV(NSVC),
     *    OSV(NSVC),ZSV(NSVC),CSV(NSVC),VSV(NSVC),
     *	  HNU(NN),HND(NN),VNU(NN),VND(NN),AIRC(NSVC))
	  IF(HNU(NN).LT.HMNSV(NSVC).OR.HNU(I).GT.HMXSV(NSVC))THEN
	  KSV(NSVC)=2
	  OSV(NSVC)=1.0
	  ISVST(NSVC)=ISVST(NSVC)+1
	  KSVST(NSVC,ISVST(NSVC))='1 TO 2'
	  TSVST(NSVC,ISVST(NSVC))=T
	  IF(HNU(NN).LT.HMNSV(NSVC))THEN
	  IPILOT(NSVC,ISVST(NSVC))=1
	  ELSE
	  IPILOT(NSVC,ISVST(NSVC))=2
	  ENDIF
	  WRITE(4,1260)NSVC,T,HNU(NN)
1260	  FORMAT(2X,'*** SURGE VALVE STATUS CHANGE FROM CLOSING '
     *    'TO OPEN',2X,'NSVC=',I3,2X,'TIME=',F8.3,2X,'HNU=',F6.3)
          CALL SVALVE(B(IIP),AR(IIP),CP,CM,KSV(NSVC),
     *    OSV(NSVC),ZSV(NSVC),CSV(NSVC),VSV(NSVC),
     *	  HNU(NN),HND(NN),VNU(NN),VND(NN),AIRC(NSVC))
	  END IF
	  IF(VSV(NSVC).GT.0.0)QSV(NSVC)=QSV(NSVC)+VSV(NSVC)*DT 
	ELSE
	  OSV(NSVC)=1.0
          CALL SVALVE(B(IIP),AR(IIP),CP,CM,KSV(NSVC),
     *    OSV(NSVC),ZSV(NSVC),CSV(NSVC),VSV(NSVC),
     *	  HNU(NN),HND(NN),VNU(NN),VND(NN),AIRC(NSVC))
	  IF(HNU(NN).GE.HMNSV(NSVC).AND.HNU(I).LE.HMXSV(NSVC))THEN
	  KSV(NSVC)=1
	  TSV(NSVC)=T
	  ISVST(NSVC)=ISVST(NSVC)+1
	  KSVST(NSVC,ISVST(NSVC))='2 TO 1'
	  TSVST(NSVC,ISVST(NSVC))=T
	  IPILOT(NSVC,ISVST(NSVC))=0
	  WRITE(4,1270)NSVC,T,HNU(NN)
1270	  FORMAT(2X,'*** SURGE VALVE STATUS CHANGE FROM OPEN TO ' 
     *    'CLOSING',2X,'NSVC=',I3,2X,'TIME=',F8.3,2X,'HNU='F6.3)
	  END IF
	  IF(VSV(NSVC).GT.0.0)QSV(NSVC)=QSV(NSVC)+VSV(NSVC)*DT 
	END IF
	IPTSV=IPTSV+1
	NSVC=IORSV(IPTSV)
	ICY=1
	END IF


	IF (ICY.EQ.0.AND.NPSS(NSSC).EQ.IIP.AND.ISS(NSSC).EQ.II)THEN                      
	KODEA=0
	FNR=B(IIP)*SSCK(NSSC)*HSS(NSSC)+CM+CP-B(IIP)*
     *  (VD(NN)-VU(NN))
	DR=2.0+B(IIP)*SSCK(NSSC)
	HNU(NN)=FNR/DR
	HND(NN)=HNU(NN)
	HNSS(NSSC)=HNU(NN)
	IF(HNSS(NSSC).GT.HTSS(NSSC))THEN
	HNSS(NSSC)=HTSS(NSSC)
	HNU(NN)=HTSS(NSSC)
	HND(NN)=HTSS(NSSC)
	ELSE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 HND(NN)=CM
	 HNU(NN)=CP-B(IIP)*VNU(NN)
	ELSEIF(TAIV.GT.0.001.AND.TABIV.LE.0.001)THEN
	 CPM=CP-CM
	 BB=2*B(IIP)*TAIV**2
	 CC=TAIV**2*CPM
	 IF(CPM.GE.0)THEN
	 VND(NN)=(-BB+SQRT(BB*BB+4*HLIV*CC))/(2*HLIV)
	 ELSE
	 VND(NN)=(BB-SQRT(BB*BB-4*HLIV*CC))/(2*HLIV)
	 ENDIF
	 VBYIV=0.0
	 VNU(NN)=VND(NN)
	 HNU(NN)=CP-B(IIP)*VNU(NN)
	 HND(NN)=CM+B(IIP)*VND(NN)
	ELSEIF(TAIV.LE.0.001.AND.TABIV.LE.0.001)THEN
	 VBYIV=0.0
	 VND(NN)=0.0
	 VNU(NN)=0.0
	 HNU(NN)=CP
	 HND(NN)=CM
	ENDIF
	QIV=QIV+0.5*DT*(VND(NN)+VD(NN))
	QBIV=QBIV+0.5*DT*(VBYIV+VNU(NN)-VND(NN))
	ICY=1
	ENDIF	  


 	IF (ICY.EQ.0) THEN
	HNU(NN)=(CM+CP)/2
        HND(NN)=HNU(NN)
        VNU(NN)=(CP-HNU(NN))/B(IIP)
	VND(NN)=VNU(NN)
	ENDIF

1500    CONTINUE
1600	CONTINUE


	DO IIP=1,NPMAX
        DO II=1,NEND(IIP)
	NN=NSTART(IIP)+II-1
        IF(HNU(NN).LT.HUMIN(NN))THEN
        HUMIN(NN)=HNU(NN)
        TUMIN(NN)=T
	ENDIF
        IF(HND(NN).LT.HDMIN(NN))THEN
        HDMIN(NN)=HND(NN)
        TDMIN(NN)=T
	ENDIF
        IF(HNU(NN).GT.HUMAX(NN))THEN
        HUMAX(NN)=HNU(NN)
        TUMAX(NN)=T
	ENDIF
        IF(HND(NN).GT.HDMAX(NN))THEN
        HDMAX(NN)=HND(NN)
        TDMAX(NN)=T
	ENDIF
        IF(VNU(NN).LT.VUMIN(NN))THEN
        VUMIN(NN)=VNU(NN)
        TVUMN(NN)=T
	ENDIF
        IF(VND(NN).LT.VDMIN(NN))THEN
        VDMIN(NN)=VND(NN)
        TVDMN(NN)=T
	ENDIF
        IF(VNU(NN).GT.VUMAX(NN))THEN
        VUMAX(NN)=VNU(NN)
        TVUMX(NN)=T
	ENDIF
        IF(VND(NN).GT.VDMAX(NN))THEN
        VDMAX(NN)=VND(NN)
        TVDMX(NN)=T
	ENDIF
	END DO
	END DO
 
	IF(NPMP.GT.0)THEN
	DO IP=1,NPMP
	IF(AL(IP).LT.ALMIN(IP))THEN
        ALMIN(IP)=AL(IP)
        TALMIN(IP)=T
	ENDIF
        IF(AL(IP).GT.ALMAX(IP))THEN
        ALMAX(IP)=AL(IP)
        TALMAX(IP)=T
	ENDIF
        IF(BET(IP).LT.BETMIN(IP))THEN
        BETMIN(IP)=BET(IP)
        TBETMN(IP)=T
	ENDIF
        IF(BET(IP).GT.BETMAX(IP))THEN
        BETMAX(IP)=BET(IP)
        TBETMX(IP)=T
	ENDIF
        IF(HPU(IP).LT.HPUMIN(IP))THEN
        HPUMIN(IP)=HPU(IP)
        THPUMN(IP)=T
	ENDIF
        IF(HPU(IP).GT.HPUMAX(IP))THEN
        HPUMAX(IP)=HPU(IP)
        THPUMX(IP)=T
	ENDIF
        IF(VPU(IP).LT.VPUMIN(IP))THEN
        VPUMIN(IP)=VPU(IP)
        TVPUMN(IP)=T
	ENDIF
        IF(VPU(IP).GT.VPUMAX(IP))THEN
        VPUMAX(IP)=VPU(IP)
        TVPUMX(IP)=T
	ENDIF
	END DO
	ENDIF

	IF(NBST.GT.0)THEN
	DO IB=1,NBST
	IF(ALB(IB).LT.ALMINB(IB))THEN
        ALMINB(IB)=ALB(IB)
        TALMNB(IB)=T
	ENDIF
        IF(ALB(IB).GT.ALMAXB(IB))THEN
        ALMAXB(IB)=ALB(IB)
        TALMXB(IB)=T
	ENDIF
        IF(BETB(IB).LT.BETMNB(IB))THEN
        BETMNB(IB)=BETB(IB)
        TBTMNB(IB)=T
	ENDIF
        IF(BETB(IB).GT.BETMXB(IB))THEN
        BETMXB(IB)=BETB(IB)
        TBTMXB(IB)=T
	ENDIF
        IF(HPUB(IB).LT.HPUMNB(IB))THEN
        HPUMNB(IB)=HPUB(IB)
        THPMNB(IB)=T
	ENDIF
        IF(HPUB(IB).GT.HPUMXB(IB))THEN
        HPUMXB(IB)=HPUB(IB)
        THPMXB(IB)=T
	ENDIF
        IF(VPUB(IB).LT.VPUMNB(IB))THEN
        VPUMNB(IB)=VPUB(IB)
        TVPMNB(IB)=T
	ENDIF
        IF(VPUB(IB).GT.VPUMXB(IB))THEN
        VPUMXB(IB)=VPUB(IB)
        TVPMXB(IB)=T
	ENDIF
	END DO
	ENDIF

	IF(NSV.GT.0)THEN
	DO I=1,NSV
	IF(VSV(I).LT.VSVMN(I))THEN
	VSVMN(I)=VSV(I)
	TVSVMN(I)=T
	END IF
	IF(VSV(I).GT.VSVMX(I))THEN
	VSVMX(I)=VSV(I)
	TVSVMX(I)=T
	END IF
	END DO
	ENDIF

	IF(NSS.GT.0)THEN
	DO I=1,NSS
	IF(HNSS(I).GT.HSSMAX(I))THEN
	HSSMAX(I)=HNSS(I)
	TSSMAX(I)=T
	ENDIF
	IF(HNSS(I).LT.HSSMIN(I))THEN
	HSSMIN(I)=HNSS(I)
	TSSMIN(I)=T
	ENDIF
	IF(QAIR(I).GT.QAIRMX(I))QAIRMX(I)=QAIR(I)
	END DO
	ENDIF

	IF(CODECS.EQ.'YES')THEN
	DO I=1,NCS
        IF(VCAV(I).GT. VCAVMX(I))THEN
        VCAVMX(I)=VCAV(I)
        TCAVMX(I)=T
	ENDIF
	NN=NSTART(NPCS(I))+ICS(I)-1
        DVCAV=VNU(NN)-VND(NN)
        IF(DVCAV.LT.DVCVMN(I))THEN
        DVCVMN(I)=DVCAV
        TDVCMN(I)=T
	ENDIF
        IF(DVCAV.GT.DVCVMX(I))THEN
        DVCVMX(I)=DVCAV
        TDVCMX(I)=T
	ENDIF
	END DO
	ENDIF

	IF(CODEAC.EQ.'YES')THEN
        IF(C.LT.CMIN)THEN
        CMIN=C
        TCMIN=T
	ENDIF
        IF(C.GT.CMAX)THEN
        CMAX=C
        TCMAX=T
	ENDIF
        IF(HAC.LT.HACMIN)THEN
        HACMIN=HAC
        THACMN=T
	ENDIF
        IF(HAC.GT.HACMAX)THEN
        HACMAX=HAC
        THACMX=T
	ENDIF
        IF(VAC.LT.VACMIN)THEN
        VACMIN=VAC
        TVACMN=T
	ENDIF
        IF(VAC.GT.VACMAX)THEN
        VACMAX=VAC
        TVACMX=T
	ENDIF
	ENDIF

	IF(NOST.GT.0)THEN
	DO I=1,NOST
        IF(HOST(I).LT.HOSTMN(I))THEN
        HOSTMN(I)=HOST(I)
        TOSTMN(I)=T
	ENDIF
        IF(HOST(I).GT.HOSTMX(I))THEN
        HOSTMX(I)=HOST(I)
        TOSTMX(I)=T
 	ENDIF
	IF(VOST(I).GT.VOSTMX(I))THEN
	VOSTMX(I)=VOST(I)
	TVOSTM(I)=T
	ENDIF
	END DO 
	ENDIF


        DO IIP=1,NPMAX
	DO II=1,NEND(IIP)
	NN=NSTART(IIP)+II-1
        VU(NN)=VNU(NN)
        VD(NN)=VND(NN)
        HU(NN)=HNU(NN)
        HD(NN)=HND(NN)
	END DO
	END DO
        IF(CODEAC.EQ.'YES')CO=C
	IF(NOST.GT.0)THEN
	DO I=1,NOST
        HOSTO(I)=HOST(I)
	END DO
	ENDIF
	IF(NSS.GT.0)THEN
	DO I=1,NSS
	HSS(I)=HNSS(I)
	END DO
	ENDIF

        JPCR=JPCR+1
        GO TO 1000

2000    CONTINUE

	IF(NPMP.GT.0)THEN
	WRITE(4,2005)
2005	FORMAT(/2X,'PUMP DETAILS')	
	DO IP=1,NPMP 
        WRITE (4,2010) IP,TVO(IP),ALMIN(IP),TALMIN(IP),ALMAX(IP),
     *  TALMAX(IP),BETMIN(IP),TBETMN(IP),BETMAX(IP),TBETMX(IP)
2010    FORMAT(/2X,'IP=',I2,2X,'TVO=',F8.3,1X,'ALMIN=',F6.3,1X,
     *  'TALMIN=',F7.3,1X,
     *       'ALMAX=',F6.3,1X,'TALMAX=',F7.3,1X,'BETMIN=',F6.3,1X,
     *       'TBTMN=',F7.3,1X,'BETMAX=',F6.3,1X,'TBTMX=',F7.3/)
        WRITE (4,2020) HPUMIN(IP),THPUMN(IP),HPUMAX(IP),THPUMX(IP),
     *  VPUMIN(IP),TVPUMN(IP),VPUMAX(IP),TVPUMX(IP)
2020    FORMAT(2X,'HPUMIN=',F6.3,1X,'THPUMIN=',F7.3,1X,'HPUMAX=',F6.3,
     *    1X,'THPUMAX=',F7.3,1X,'VPUMIN=',F6.3,1X,'TVPUMIN=',F7.3,1X,
     *    'VPUMAX=',F6.3,1X,'TVPUMAX=',F7.3/)
	END DO
	ENDIF
	IF(NBST.GT.0)THEN
	WRITE(4,2025)
2025	FORMAT(/2X,'BOOSTER PUMP DETAILS'/)	
	DO IP=1,NBST 
        WRITE (4,2010)IP,TVOB(IP),ALMINB(IP),TALMNB(IP),ALMAXB(IP),
     *  TALMXB(IP),BETMNB(IP),TBTMNB(IP),BETMXB(IP),TBTMXB(IP)
        WRITE (4,2020)HPUMNB(IP),THPMNB(IP),HPUMXB(IP),THPMXB(IP),
     *  VPUMNB(IP),TVPMNB(IP),VPUMXB(IP),TVPMXB(IP)
	END DO
	ENDIF
	IF(CODEAC.EQ.'YES')THEN
        WRITE(4,2030)CMIN,TCMIN,CMAX,TCMAX,HACMIN,THACMN,HACMAX,THACMX
2030    FORMAT(2X,'CMIN=',F6.3,1X,'TCMIN=',F7.3,1X,'CMAX=',
     *        F6.3,1X,'TCMAX=',F7.3,1X,'HACMIN=',F6.3,1X,
     *        'THACMN=',F7.3,1X,'HACMAX=',F6.3,1X,'THACMX=',F7.3)
        WRITE (4,2050) TVOAC,VACMIN,TVACMN,VACMAX,TVACMX
2050    FORMAT(/2X,'TVOAC=',F8.3,2X,'VACMIN=',F6.3,1X,'TVACMIN=',
     *  F7.3,1X,'VACMAX=',F6.3,1X,'TVACMAX=',F7.3)
	ENDIF
	IF(NOST.GT.0)THEN
	WRITE(4,2060)
2060	FORMAT(/2X,'NPOST',2X,'HOSTMN',2X,'TOSTMIN',2X,'HOSTMX',2X,
     *  'TOSTMAX',3X,'TDOST',2X,'VOSTMX',2X,'TVOSTMX'/)
	DO 2070 I=1,NOST
2070	WRITE (4,2080)NPOST(I),HOSTMN(I),TOSTMN(I),HOSTMX(I),TOSTMX(I)
     *   ,TDOST(I),VOSTMX(I),TVOSTM(I)
2080	FORMAT (2X,I4,2X,F6.3,2X,F7.3,2X,F6.3,2X,F7.3,2X,
     *   F7.3,2X,F6.3,2X,F7.3)
	ENDIF
	IF(NNRV.GT.0)THEN
        WRITE (4,2090) (TVONRV(I),I=1,NNRV)
2090    FORMAT(2X,'TVONRV:',10F8.3)
	ENDIF
	IF(CODECS.EQ.'YES')THEN
	DO I=1,NCS
        WRITE (4,2100) VCAVMX(I),TCAVMX(I),DVCVMN(I),TDVCMN(I),
     *  DVCVMX(I),TDVCMX(I)
2100    FORMAT(/2X,'VCAVMAX=',F9.5,1X,'TVCAVMAX=',F7.3,1X,'DVCAVMIN=',
     *        F9.5,1X,'TDVCAVMIN=',F7.3,1X,'DVCAVMAX=',F9.5,1X,
     *        'TDVCAVMAX=',F7.3)
	END DO
	ENDIF
	IF(NSV.GT.0)THEN
	WRITE(4,2110)
2110	FORMAT(/2X,'NPSV',5X,'VSVMN',4X,'TVSVMN',5X,'VSVMX',4X,
     *          'TVSVMX',6X,'QSV'/)
        DO 2120 I=1,NSV
2120	WRITE(4,2130)NPSV(I),VSVMN(I),TVSVMN(I),VSVMX(I),TVSVMX(I),
     *  QSV(I)
2130	FORMAT(2X,I3,5F10.4)      		  
	ENDIF
	IF(NSS.GT.0)THEN
	WRITE(4,2140)
2140	FORMAT(/2X,'NPSS',2X,'ISS',4X,'HSSMIN',4X,'TSSMIN',
     *	      4X,'HSSMAX',4X,'TSSMAX',2X,'VOLAIR',2X,'QAIRMX'/)
	DO I=1,NSS
	WRITE(4,2150)NPSS(I),ISS(I),HSSMIN(I),TSSMIN(I),HSSMAX(I),
     *  TSSMAX(I),VOLAIR(I),QAIRMX(I)
2150	FORMAT(3X,I3,3X,I3,6F10.4)
	END DO
	ENDIF
	IF(CODIV.EQ.'YES')THEN
	WRITE(4,2155)QIV,QBIV
2155	FORMAT(/2X,'QIV=',F8.3,2X,'QBIV=',F8.3)
	ENDIF

	WRITE (8,2165)REFH,REFV,REFT
2165	FORMAT(/4X,'RESULTS FOR HEAD AND VELOCITY IN NONDIMENSIONAL ',
     *  'FORM AT'/4X,'NODES CORRESPONDING TO SPECIFIED CHAINAGES.'//4X,
     *  'REFERENCE VALUES FOR NONDIMENSIONALISATION:'/4X,
     *  'REFERNCE HEAD (m) =',F6.1/4X,'REFERENCE VELOCITY (m/sec) =',
     *  F6.3/4X,'REFERENCE TIME (sec) =',F6.2) 
        WRITE (8,2170)
2170    FORMAT(/4X,'NP',3X,'NN',2X,'HUMIN',2X,'TUMIN',2X,'HUMAX',2X,
     *  'TUMAX',2X,'HDMIN',2X,'TDMIN',2X,'HDMAX',2X,'TDMAX',2X,'VUMIN',
     *      2X,'TVUMN',2X,'VUMAX',2X,'TVUMX',2X,'VDMIN',2X,'TVDMN',
     *      2X,'VDMAX',2X,'TVDMX'/)
        DO II=1,NPRNT
	NP=NPPRNT(II)
	IN=IPRNT(II)
	NN=NNPRNT(II)
        WRITE (8,2200)NP,IN,HUMIN(NN),TUMIN(NN),HUMAX(NN),TUMAX(NN),
     *  HDMIN(NN),TDMIN(NN),HDMAX(NN),TDMAX(NN),VUMIN(NN),TVUMN(NN),
     *  VUMAX(NN),TVUMX(NN),VDMIN(NN),TVDMN(NN),VDMAX(NN),TVDMX(NN)
2200    FORMAT(2X,I3,3X,I3,16F7.3)
	END DO

	WRITE (4,2220)
2220	FORMAT(/4X,'NP',3X,'IN',3X,'HUMIN(m)',3X,'HUMAX(m)',3X,
     *	        'HDMIN(m)',3X,'HDMAX(m)'/)
        DO II=1,NPRNT
	NP=NPPRNT(II)
	IN=IPRNT(II)
	NN=NNPRNT(II)
	WRITE (4,2230) NP,IN,
     *	      (HUMIN(NN)*REFH+DATUM),(HUMAX(NN)*REFH+DATUM),
     *	      (HDMIN(NN)*REFH+DATUM),(HDMAX(NN)*REFH+DATUM)
2230	FORMAT (2X,I3,3X,I3,F9.3,2X,F9.3,2X,F9.3,2X,F9.3)
	END DO


	IF(NPMP.GT.0)THEN
	DO I=1,NPMP
	TVOD(I)=TVO(I)*REFT
	VPUMIN(I)=100*VPUMIN(I)
	IF(KODPHV(I).EQ.1.AND.DLY(I).LT.0.001.AND.VPUMIN(I).LT.0)
     *  VPUMIN(I)=0.0 
	VPUMAX(I)=100*VPUMAX(I)
	HPUMIN(I)=100*HPUMIN(I)
	HPUMAX(I)=100*HPUMAX(I)
	ALMIN(I)=100*ALMIN(I)
	END DO
	ENDIF

	IF(NBST.GT.0)THEN
	DO I=1,NBST
	TVODB(I)=TVOB(I)*REFT
	VPUMNB(I)=100*VPUMNB(I)
	IF(KODBSV(I).EQ.1.AND.DLYB(I).LT.0.001.AND.VPUMNB(I).LT.0)
     *  VPUMNB(I)=0.0 
	VPUMXB(I)=100*VPUMXB(I)
	HPUMNB(I)=100*HPUMNB(I)
	HPUMXB(I)=100*HPUMXB(I)
	ALMINB(I)=100*ALMINB(I)
	END DO
	ENDIF

	DO I=1, NPRNT
	NN=NNPRNT(I)
	HMIND(I)=HDMIN(NN)*REFH+DATUM
	HMAXD(I)=HDMAX(NN)*REFH+DATUM
	END DO

	IF (CODECS.EQ.'YES') THEN
	DO I=1,NCS
	VCAVMX(I)=VCAVMX(I)*REFT*PDC(NPREF)*AR(NPCS(I))
	TCAVMX(I)=TCAVMX(I)*REFT
	DVCVMX(I)=DVCVMX(I)*REFV
	TDVCMX(I)=TDVCMX(I)*REFT
	END DO
	END IF

	IF (CODEAC.EQ.'YES') THEN
	CO=(PDC(NPREF)/2)*REFT*AR(NPAC)*ACC
	CMAX=CMAX*CO
	TCMAX=TCMAX*REFT
	CMIN=CMIN*CO
	TCMIN=TCMIN*REFT
	VCACMX=VACMAX*REFV*(PDIA(NPAC)/DCAC)**2
	IF (KACTYP.EQ.1) THEN
	VORAC=VCACMX*(DCAC/DORIF)**2
	ELSEIF (KACTYP.EQ.2) THEN
	VBYAC=VACMIN*REFV*(PDIA(NPAC)/DBYAC)**2
	ENDIF
	ENDIF

	IF (NOSTD.GT.0) THEN
	DO I=1,NOSTD
	DRGOST(I)=(HTOST(I)-HOSTMN(I))*REFH
	IF(TDOST(I).LT.999)THEN
	TDRG(I)=TDOST(I)*REFT
	ELSE
	CTDRG(I)='NO FULL DRG'
	ENDIF
	VOSTMX(I)=VOSTMX(I)*REFV*(PDIA(NPOST(I))/DCOST(I))**2
	END DO
	END IF

	IF (NSV.GT.0) THEN
	DO I=1,NSV
	QSVD(I)=QSV(I)*PDC(NPREF)*REFT
	SIZSVM=SIZSV(I)/1000
	VSVMXD(I)=VSVMX(I)*REFV*(PDIA(NPREF)/SIZSVM)**2/NNSV(I)
	TVSVD(I)=TVSVMX(I)*REFT
	IF (ISVST(I).GT.0) THEN
	 DO KK=1,ISVST(I)
	 TSVSTD(I,KK)=TSVST(I,KK)*REFT
	 IF(IPILOT(I,KK).EQ.1)THEN
	 PILOT(I,KK)='LOW PRESSURE PILOT'
	 ELSEIF(IPILOT(I,KK).EQ.2)THEN
	 PILOT(I,KK)='HIGH PRESSURE PILOT'
	 ENDIF
	 END DO
	ENDIF
	END DO
	ENDIF
	IF(KODEDS.GT.0)QEXITD=QEXIT*REFT*PDC(NPREF)

	IF(CODIV.EQ.'YES')THEN
	QIVD=QIV*PDC(NPREF)*REFT*AR(NPIV)
	QBIVD=QBIV*PDC(NPREF)*REFT*AR(NPIV)
	ENDIF


	WRITE(5,2500)
2500	FORMAT(/2X,'RESULTS OF ANALYSIS'//2X,'NOTES:'/2X,
     *'1.Where moderate to extensive occurrence of vapour pressure'/3X,
     *' is indicated by analysis, the results are to be treated '/3X,
     *' as of qualitative value only.'/2X,
     *'2.Where air valves/air cusion valves are used as vacuum breaker'
     * /4x,'the results are to be treated as approximate.'/2X,
     *'3.The User Manual may be referred for more details regarding'
     * /4x,'limitations of analysis and for preparing results in'
     */4X,'graphical form.')

	IF(NPMP.GT.0)THEN
	WRITE(5,2600)
2600	FORMAT(//2X,'DETAILS REGARDING PUMPS'/)
	DO I=1,NPMP
	IF(ITRIP(I).EQ.1)THEN
	WRITE(5,2605)
2605	FORMAT(2X,'FAILING PUMP')
	IF(SIML.EQ.'TYPEB')WRITE(5,2607)I
2607	FORMAT(/2X,
     *  'PUMP NUMBER                                       =', I3)     
	WRITE(5,2610)TVOD(I),VPUMIN(I),HPUMIN(I),HPUMAX(I),ALMIN(I)
2610	FORMAT(2X,
     *	'TIME OF FLOW REVERSAL (sec)                       =', F6.2/2X,
     *	'MINIMUM FLOW OF PUMP (%)                          =', F6.1/2X,
     *	'MINIMUM HEAD OF PUMP (%)                          =', F6.1/2X,
     *	'MAXIMUM HEAD OF PUMP (%)                          =', F6.1/2X,
     *	'MINIMUM SPEED OF PUMP (%)                         =', F6.1/)
	ELSE
	WRITE(5,2615)
2615	FORMAT(2X,'RUNNING PUMP')
	IF(SIML.EQ.'TYPEB')WRITE(5,2607)I
	WRITE(5,2620)VPUMAX(I),HPUMIN(I),HPUMAX(I)
2620	FORMAT(2X,
     *	'MAXIMUM FLOW OF PUMPS (%)                         =', F6.1/2X,
     *	'MINIMUM HEAD OF PUMPS (%)                         =', F6.1/2X,
     *	'MAXIMUM HEAD OF PUMPS (%)                         =', F6.1/)
	ENDIF
	END DO
	ENDIF

	IF(NBST.GT.0)THEN
	WRITE(5,2625)
2625	FORMAT(//2X,'DETAILS REGARDING BOOSTER PUMPS'/)
	DO I=1,NBST
	IF(ITRIPB(I).EQ.1)THEN
	WRITE(5,2630)
2630	FORMAT(2X,'FAILING BOOSTER PUMP')
	WRITE(5,2635)I,TVODB(I),VPUMNB(I),HPUMNB(I),HPUMXB(I),
     *  ALMINB(I)
2635	FORMAT(9X/2X,
     *  'BOOSTER PUMP NUMBER                               =', I3/2X,
     *	'TIME OF FLOW REVERSAL (sec)                       =', F6.2/2X,
     *	'MINIMUM FLOW OF BOOSTER (%)                       =', F6.1/2X,
     *	'MINIMUM HEAD OF BOOSTER (%)                       =', F6.1/2X,
     *	'MAXIMUM HEAD OF BOOSTER (%)                       =', F6.1/2X,
     *	'MINIMUM SPEED OF BOOSTER (%)                      =', F6.1/)
	ELSE
	WRITE(5,2640)
2640	FORMAT(2X,'RUNNING BOOSTER PUMP')
	WRITE(5,2645)I,VPUMXB(I),HPUMNB(I),HPUMXB(I)
2645	FORMAT(9X/2X,
     *  'BOOSTER PUMP NUMBER                               =', I3/2X,
     *	'MAXIMUM FLOW OF BOOSTER (%)                       =', F6.1/2X,
     *	'MINIMUM HEAD OF BOOSTER (%)                       =', F6.1/2X,
     *	'MAXIMUM HEAD OF BOOSTER (%)                       =', F6.1/)
	ENDIF
	END DO
	ENDIF

	IF(CODEAC.EQ.'YES')THEN
	WRITE(5,2650)
2650	FORMAT(/2X, 'DETAILS REGARDING AIR VESSEL')
	IF(KACTYP.EQ.1)THEN 
	WRITE(5,2660) CO,CMAX,TCMAX,CMIN,TCMIN,VCACMX,VORAC
2660	FORMAT(/2X,
     *  'AIR VOLUME UNDER WORKING CONDITION (cum)          =', F7.2/2X,
     *  'MAXIMUM AIR VOLUME UNDER SURGE (cum)              =', F7.2/2X,
     *	'TIME OF OCCURENCE OF MAXIMUM AIR VOLUME (sec)     =', F6.1/2X,
     *  'MINIMUM AIR VOLUME UNDER SURGE (cum)              =', F7.2/2X,
     *	'TIME OF OCCURRENCE OF MINIMUM AIR VOLUME (sec)    =', F6.1/2X,
     *	'MAXIMUM VELOCITY THROUGH CONNECTING PIPE (m/sec)  =', F5.2/2X,
     *	'MAXIMUM VELOCITY THROUGH ORIFICE (m/sec)          =', F5.1/)
	ELSEIF(KACTYP.EQ.2)THEN
	WRITE(5,2670) CO,CMAX,TCMAX,CMIN,TCMIN,VCACMX,VBYAC
2670	FORMAT(/2X,
     *	'AIR VOLUME UNDER WORKING CONDITION (cum)          =', F6.2/2X,
     *	'MAXIMUM AIR VOLUME UNDER SURGE (cum)              =', F6.2/2X,
     *	'TIME OF OCCURENCE OF MAXIMUM AIR VOLUME (sec)     =', F6.1/2X,
     *	'MINIMUM AIR VOLUME UNDER SURGE (cum)              =', F6.2/2X,
     *	'TIME OF OCCURRENCE OF MINIMUM AIR VOLUME (sec)    =', F6.1/2X,
     *	'MAXIMUM VELOCITY THROUGH CONNECTING PIPE (m/sec)  =', F5.2/2X,
     *	'MAXIMUM VELOCITY THROUGH BYPASS (m/sec)           =', F5.1/)
	ENDIF
	ENDIF

	IF(NOSTD.GT.0)THEN
	WRITE(5,2680)
2680	FORMAT(/2X,'DETAILS REGARDING ONE WAY SURGE TANKS'/)
	WRITE(5,2690)
2690	FORMAT(2X,'TANK NO.' ,2X,'STORAGE',3X,'EXTENT OF',2X,
     *  'TIME OF COMPLETE',2X,'MAX VELOCITY IN'/12X,'DEPTH(m)',
     *  2X,'DRAINING(m)',2X,'DRAINING (Sec)',2X,'CONN. PIPE(m/sec)'/)
	DO I=1,NOSTD
	IF(TDOST(I).LT.999)THEN
	WRITE(5,2700) I,YSTOST(I),DRGOST(I),TDRG(I),VOSTMX(I)
2700	FORMAT(5X,I2,6X,F5.2,6X,F5.2,9X,F6.1,11X,F5.2)
	ELSE
	WRITE(5,2710) I,YSTOST(I),DRGOST(I),CTDRG(I),VOSTMX(I)
2710	FORMAT(5X,I2,6X,F5.2,6X,F5.2,5X,A12,9X,F5.2)
	END IF
	END DO
	ENDIF

	IF(NSV.GT.0)THEN
	WRITE(5,2712)
2712	FORMAT(/2X,'DETAILS REGARDING SURGE RELIEF VALVE'/)
	DO I=1,NSV
	IF(SIML.EQ.'TYPEB')WRITE(5,2713)I
2713	FORMAT(2X,
     *  'SURGE RELIEF VALVE NUMBER                         =',I2)
	WRITE(5,2715)VSVMXD(I),TVSVD(I),QSVD(I)
2715	FORMAT(2X,	
     *  'MAXIMUM VELOCITY THROUGH VALVE (m/sec)            =',F5.1/2X,
     *  'TIME OF OCCURENCE OF MAXIMUM VELOCITY (sec)       =',F6.1/2X,
     *  'CUMULATIVE DISCHARGE THROUGH THE VALVE (cum)      =',F7.3)
	WRITE(5,2720)
2720	FORMAT(/2X,'DETAILS REGARDING STATUS CHANGE OF SURGE ',
     *  'RELIEF VALVE')
	IF(ISVST(I).GT.0)THEN
	 DO KK=1,ISVST(I)
	 IF(KSVST(I,KK).EQ.'0 TO 2')THEN
	 WRITE(5,2730)TSVSTD(I,KK),PILOT(I,KK)
2730	 FORMAT(/6X,'VALVE STATUS CHANGES FROM CLOSED TO OPEN STATUS'/
     *   8X,'TIME(sec)=',F6.1/8X,'PILOT ACTION: ',A20)
	 ELSEIF(KSVST(I,KK).EQ.'1 TO 2')THEN
	 WRITE(5,2740)TSVSTD(I,KK),PILOT(I,KK)
2740	 FORMAT(/6X,'VALVE STATUS CHANGES FROM PARTIALLY CLOSED TO ',
     *   'OPEN STATUS'/8X,'TIME(sec)=',F6.1/8X,'PILOT ACTION: ',A20)	    	
	 ELSEIF(KSVST(I,KK).EQ.'2 TO 1')THEN
	 WRITE(5,2750)TSVSTD(I,KK)
2750	 FORMAT(/6X,'VALVE STATUS CHANGES FROM OPEN TO ',
     *   'CLOSING STATUS'/8X,'TIME(sec)=',F6.1)
	 ENDIF
	 END DO
	ELSE
	 WRITE(5,2760)
2760	 FORMAT(/2X,'SURGE RELIEF VALVE DOES NOT OPEN AT ALL')	 
	ENDIF
	WRITE(5,2770)
2770	FORMAT(2X/) 
	END DO
	ENDIF

	IF(CODECS.EQ.'YES')THEN
	DO I=1,NCS
	WRITE(5,2800)
2800	FORMAT(/2X,'DETAILS REGARDING WATER COLUMN SEPERATION')
	IF(SIML.EQ.'TYPEB')WRITE(5,2805)NPCS(I)
2805	FORMAT(/2X,
     *  'PIPE NUMBER AT COLUMN SEPARATION LOCATION         =',I3)
	WRITE(5,2810)CHCS(I),VCAVMX(I),TCAVMX(I),DVCVMX(I),TDVCMX(I)
2810	FORMAT(2X,
     *  'LOCATION OF COLUMN SEPERATION (Ch.m)              =',F7.0/2X,
     *	'MAXIMUM CAVITY VOLUME (cum)                       =', F7.3/2X,
     *	'TIME OF OCCURRENCE OF MAX CAVITY VOL. (sec)       =', F6.1/2X,
     *	'MAXIMUM VELOCITY DIFFERENTIAL AT CAVITY (m/sec)   =', F5.2/2X,
     *	'TIME OF OCCURENCE OF MAX VELOCITY DIFERENTIAL(sec)=', F6.1/)
	END DO
	END IF

	IF(KODEDS.GT.0)THEN
	WRITE(5,2820)QEXITD
2820	FORMAT(/2X,
     *  'TOTAL OUTFLOW AT RESERVOIR TILL VALVE CLOSURE(cum)=',F8.1/)
	ENDIF

	IF(CODIV.EQ.'YES')THEN
	WRITE(5,2830)QIVD
2830	FORMAT(/2X,
     *  'OUTFLOW THROUGH INTERMEDIATE VALVE (cum)          =',F7.1)
	IF(CODBIV.EQ.'YES')THEN
	WRITE(5,2835)QBIVD
2835	FORMAT(/2X,
     *  'OUTFLOW THROUGH BYPASS OF INTERMEDIATE VALVE (cum)=',F7.1)
	ENDIF
	ENDIF

	WRITE(5,2840)
2840	FORMAT(/2X,'MINIMUM & MAXIMUM PIEZOMETRIC HEADS AT ' 
     *  'SPECIFIED CHAINAGES'/)
	IF(SIML.EQ.'TYPEA')THEN
	WRITE(5,2845)
2845	FORMAT(10X,'SPECIFIED',2X,'NEAREST NODAL',2X,'MIN HEAD',2X,
     *  'MAX HEAD'/9X,'CHAINAGE(m)',2X,'CHAINAGE(m)',4X,'(RL,m)',4X,
     *  '(RL,m)'/)
	DO I=1,NPRNT
	WRITE(5,2850) CHPRNT(I),CHNODE(I),HMIND(I),HMAXD(I)
2850	FORMAT(11X,F7.1,6X,F7.1,5X,F7.2,4X,F7.2)
	END DO
	ELSE
	WRITE(5,2860)
2860	FORMAT(10X,'PIPE',2X,'SPECIFIED',2X,'NEAREST NODAL',2X,
     *  'MIN HEAD',2X,'MAX HEAD'/8X,'NUMBER',2X,'CHAINAGE(m)',2X,
     *  'CHAINAGE(m)',4X,'(RL,m)',4X,'(RL,m)'/)
	DO I=1,NPRNT
	WRITE(5,2870)NPPRNT(I),CHPRNT(I),CHNODE(I),HMIND(I),HMAXD(I)
2870	FORMAT(11X,I3,4X,F7.1,6X,F7.1,5X,F7.2,4X,F7.2)
	END DO
	ENDIF


	IF(CODEAC.EQ.'YES')THEN
	TAVV=(TVACMN+2)*REFT
	IF(TAVV.LT.20)THEN
	TAVV=20
	ELSEIF(TAVV.GT.(20*REFT))THEN
	TAVV=20*REFT
	ENDIF
	IAVV=TAVV/10+0.99
	XXAV=IAVV*10
	XNAV=0
	IF(XXAV.LE.10)THEN
	DXAV=1
	ELSEIF(XXAV.GT.10.AND.XXAV.LE.20)THEN
	DXAV=2
	ELSEIF(XXAV.GT.20.AND.XXAV.LE.50)THEN
	DXAV=4
	ELSEIF(XXAV.GT.50.AND.XXAV.LE.100)THEN
	DXAV=10
	ELSEIF(XXAV.GT.100.AND.XXAV.LE.200)THEN
	DXAV=20
	ELSE
	DXAV=50
	ENDIF	
	VACLOW=VACMIN*REFV*(PDIA(NPREF)/DCAC)**2
	IF(KACTYP.EQ.2.AND.VACMIN.LT.0)THEN
	VACLOW=VACMIN*REFV*(PDIA(NPREF)/DBYAC)**2
	ENDIF
	VACHI=VACMAX*REFV*(PDIA(NPREF)/DCAC)**2
	IYNAV=VACLOW-1
	IYXAV=VACHI+1
	YNAV=IYNAV
	YXAV=IYXAV
	DYAV=1.0
	REWIND(15)
	REWIND(25)
	WRITE(25,995)XNAV,XXAV,DXAV
	WRITE(25,995)YNAV,YXAV,DYAV
	DO II=1,TAVS
	READ(15,*)XX,YY
	WRITE(25,1010)XX,YY
	END DO	
	ENDIF

	IF(NOSTD.GT.0)THEN
	DO I=1,NOSTD
	TOSTST=TOSTS(I)*REFT
	IOSTST=TOSTST/10
	TOSTST=IOSTST*10
	TOSTED=TOSTE(I)*REFT
	IOSTED=TOSTED/10+1
	TOSTED=IOSTED*10
	XNOST=TOSTST
	XXOST=TOSTED
	IF(TOSTED.GT.XXSTAV)XXOST=XXSTAV
	GAP=XXOST-XNOST
	IF(GAP.LE.10)THEN
	DXOST=1
	ELSEIF(GAP.GT.10.AND.GAP.LE.20)THEN
	DXOST=2
	ELSEIF(GAP.GT.20.AND.GAP.LE.50)THEN
	DXOST=4
	ELSEIF(GAP.GT.50.AND.GAP.LE.100)THEN
	DXOST=10
	ELSEIF(GAP.GT.100.AND.GAP.LE.200)THEN
	DXOST=20
	ELSE
	DXOST=50
	ENDIF
	YXOST=VOSTMX(I)
	IYXOST=YXOST+1
	YXOST=IYXOST
	YNOST=0
	DYOST=0.5
	IF(I.EQ.1)THEN
	REWIND(11)
	REWIND(21)
	WRITE(21,995)XNOST,XXOST,DXOST	
	WRITE(21,995)YNOST,YXOST,DYOST	
	DO II=1,TOST1
	READ(11,*)XX,YY
	WRITE(21,1010)XX,YY
	END DO
	ELSEIF(I.EQ.2)THEN
	REWIND(12)
	REWIND(22)
	WRITE(22,995)XNOST,XXOST,DXOST	
	WRITE(22,995)YNOST,YXOST,DYOST	
	DO II=1,TOST2
	READ(12,*)XX,YY
	WRITE(22,1010)XX,YY
	END DO
	ELSEIF(I.EQ.3)THEN
	REWIND(13)
	REWIND(23)
	WRITE(23,995)XNOST,XXOST,DXOST	
	WRITE(23,995)YNOST,YXOST,DYOST	
	DO II=1,TOST3
	READ(13,*)XX,YY
	WRITE(23,1010)XX,YY
	END DO
	ELSEIF(I.EQ.4)THEN
	REWIND(14)
	REWIND(24)
	WRITE(24,995)XNOST,XXOST,DXOST	
	WRITE(24,995)YNOST,YXOST,DYOST	
	DO II=1,TOST4
	READ(14,*)XX,YY
	WRITE(24,1010)XX,YY
	END DO
	ENDIF
	END DO
	ENDIF

	IF(KODPHV(ISPLOT).EQ.1)THEN
	TPUMP=TVO(ISPLOT)*REFT+DLYPH(ISPLOT)
	ELSEIF(KODPHV(ISPLOT).EQ.2)THEN
	TPUMP=TVO(ISPLOT)*REFT+TCLOSE(ISPLOT)+DLYPH(ISPLOT)
	ELSEIF(KODPHV(ISPLOT).EQ.3)THEN
	TPUMP=TVO(ISPLOT)*REFT+TRAPID(ISPLOT)+TSLOW(ISPLOT)+
     *  DLYPH(ISPLOT)	
	ELSEIF(KODPHV(ISPLOT).EQ.4)THEN
	TPUMP=TCLOSE(ISPLOT)
	ELSEIF(KODPHV(ISPLOT).EQ.5)THEN
	TPUMP=TRAPID(ISPLOT)+TSLOW(ISPLOT)
	ELSEIF(KODPHV(ISPLOT).EQ.6)THEN
	TPUMP=TVO(ISPLOT)*REFT
	ENDIF
	ITPUMP=TPUMP+1
	TPUMP=ITPUMP
	XNSPED=0
	XXSPED=TPUMP
	IF(XXSPED.LE.10)THEN
	DXSPED=1
	ELSEIF(XXSPED.GT.10.AND.XXSPED.LE.20)THEN
	DXSPED=2
	ELSEIF(XXSPED.GT.20.AND.XXSPED.LE.50)THEN
	DXSPED=4
	ELSEIF(XXSPED.GT.50.AND.XXSPED.LE.100)THEN
	DXSPED=10
	ELSEIF(XXSPED.GT.100.AND.XXSPED.LE.200)THEN
	DXSPED=20
	ELSE
	DXSPED=50
	ENDIF
	RPMMIN=(ALMIN(ISPLOT)/100)*IPMPSP(ISPLOT)
	IF(RPMMIN.GT.0)THEN
	ISP=RPMMIN/100
	ELSE
	ISP=RPMMIN/100-1
	ENDIF
	YNSPED=100*ISP
	ISP=IPMPSP(ISPLOT)/100+1
	YXSPED=100*ISP
	DYSPED=100
	REWIND(20)
	REWIND(10)
	WRITE(20,995)XNSPED,XXSPED,DXSPED
	WRITE(20,995)YNSPED,YXSPED,DYSPED
	DO II=1,TRPM
	READ(10,*)XX,KK
	WRITE(20,1015)XX,KK
	END DO

	DO KK=1,NPATH
	 IF(KK.EQ.1)THEN
	 NWU=26
	 ELSEIF(KK.EQ.2)THEN
	 NWU=27
	 ELSEIF(KK.EQ.3)THEN
	 NWU=28	
	 ELSEIF(KK.EQ.4)THEN
	 NWU=29
	 ELSEIF(KK.EQ.5)THEN
	 NWU=30
	 ELSEIF(KK.EQ.6)THEN
	 NWU=31
	 ENDIF

	 SUM=0
	 DO II=1,NPPATH(KK)
	 SUM=SUM+PLEN(IPPATH(KK,II))
	 END DO
	 XMIN=CHAIN(IPPATH(KK,1),1)
	 XMAX=XMIN+SUM
	 DX=(XMAX-XMIN)/10
	 IF(DX.LE.10)THEN
	 IDX=DX+0.5
	 DX=IDX
	 ELSEIF(DX.GT.10.AND.DX.LE.100)THEN
	 IDX=DX/10+0.5
	 DX=IDX*10
	 ELSEIF(DX.GT.100.AND.DX.LE.1000)THEN
	 IDX=DX/100+0.5
	 DX=IDX*100
	 ELSE
	 IDX=DX/1000+0.5
	 DX=IDX*1000
	 ENDIF
	 YMIN=10000
	 DO II=1,NPPATH(KK)
	 NP=IPPATH(KK,II)
	 DO I=1,NALIGN(NP)
	 IF(GL(NP,I).LT.YMIN)YMIN=GL(NP,I)
	 END DO
	 END DO 
	 DO II=1,NPPATH(KK)
	 NP=IPPATH(KK,II)
	 DO I=1,NEND(NP)
	 NN=NSTART(NP)+I-1
	 PHMIN=HDMIN(NN)*REFH+DATUM
	 IF(PHMIN.LT.YMIN)YMIN=PHMIN
	 END DO
	 END DO
	 IF(YMIN.GT.0)THEN
	 IYMIN=YMIN/10
	 ELSE
	 IYMIN=(YMIN/10)-1
	 ENDIF
	 YMIN=IYMIN*10	
	 YMAX=-10000
	 DO II=1,NPPATH(KK)
	 NP=IPPATH(KK,II)
	 DO I=1,NEND(NP)
	 NN=NSTART(NP)+I-1
	 PHMAX=HDMAX(NN)*REFH+DATUM
	 IF(PHMAX.GT.YMAX)YMAX=PHMAX
	 END DO
	 END DO
	 IYMAX=YMAX/10+1
	 YMAX=IYMAX*10	
	 IDY=(YMAX-YMIN)/10+0.5
	 IF(IDY.LT.7)THEN
	 DY=5
	 ELSEIF(IDY.GT.15)THEN
	 DY=20
	 ELSE
	 DY=10
	 ENDIF

	 WRITE(NWU,2240)XMIN,XMAX,DX	
	 WRITE(NWU,2240)YMIN,YMAX,DY
2240	 FORMAT(50X,3F10.3)
	 NP=IPPATH(KK,1)
	 CCHST=CHAIN(NP,1)
	 DO II=1,NPPATH(KK)
	 NP=IPPATH(KK,II)
	 DO I=1,NALIGN(NP)
	 DELTAX=CHAIN(NP,I)-CHST(NP)
	 CHN=CCHST+DELTAX
	 WRITE(NWU,2250)CHN,GL(NP,I)
2250	 FORMAT(2F10.3)
	 END DO
	 CCHST=CCHST+PLEN(NP)
	 END DO

	 NP=IPPATH(KK,1)	
	 CCHST=CHAIN(NP,1)
	 DO II=1,NPPATH(KK)
	 NP=IPPATH(KK,II)
	 DO I=1,NEND(NP)
	 NN=NSTART(NP)+I-1
	 PHMIN=HDMIN(NN)*REFH+DATUM
	 PHMAX=HDMAX(NN)*REFH+DATUM
	 CHN=CCHST+FLOAT(I-1)/NGAP(NP)*PLEN(NP)
	 WRITE(NWU,2300)CHN,PHMIN,PHMAX
2300	 FORMAT(F10.3,10X,2F10.3)
	 END DO
	 CCHST=CCHST+PLEN(NP)
	 END DO
	 NP=IPPATH(KK,1)
	 CCHST=CHAIN(NP,1)
	 DO II=1,NPPATH(KK)
	 NP=IPPATH(KK,II)
	 CHEND=CCHST+PLEN(NP)
	 WRITE(NWU,2350)CCHST,HGL1(NP)
	 WRITE(NWU,2350)CHEND,HGL2(NP)
2350	 FORMAT(F10.3,30X,F10.3)
	 CCHST=CHEND
	 END DO
	END DO


C	CLOSE(UNIT=4,STATUS='DELETE')
C	CLOSE(UNIT=6,STATUS='DELETE')
	GO TO 3000

3000    STOP
        END


	SUBROUTINE FPUMP (KODPHV,IP,T,ZPU,ALO,VPUO,BETO,CI,HRAT,
     *   VDI,VPUI,AL,VPU,BET,WH,WB,DHO,CM,B,DLY,TVO,HNU,HND,VNU,
     *   VND,KI,HPU,KDNR,FKNR,CODNRR)
        CHARACTER * 10 CODNRR
	DIMENSION WH(10,89),WB(10,89),KI(20)
	DATA PI,DX,TOL,TOL1/3.1416,0.0714,0.0002,0.0001/
	IF(CODNRR.EQ.'YES')THEN
	KODNRR=1
	ELSE
	KODNRR=0
	ENDIF

10      IF(KDNR.EQ.0.)THEN

        LVB=0
        KI(IP)=1
50      X=PI+ATAN2(VPU,AL)
        I=X/DX+1
        A1=(WH(IP,I+1)-WH(IP,I))/DX
        AO=WH(IP,I+1)-A1*I*DX
        B1=(WB(IP,I+1)-WB(IP,I))/DX
        BO=WB(IP,I+1)-B1*I*DX
        HPU=(AL*AL+VPU*VPU)*(AO+A1*X)
        H1=ZPU+HPU*HRAT-DHO*VPU*ABS(VPU)/VPUI**2
        IF(LVB.EQ.1)GO TO 150
        F1=ZPU+HPU*HRAT-DHO*VPU*ABS(VPU)/VPUI**2-CM-B*VPU*(VDI/VPUI)
        F1V=2.0*VPU*(AO+A1*X)*HRAT+A1*AL*HRAT-B*(VDI/VPUI)-2.0*DHO*
     *  ABS(VPU)/VPUI**2
        F1A=2.0*AL*(AO+A1*X)*HRAT-A1*VPU*HRAT
        F2=(AL*AL+VPU*VPU)*(BO+B1*X)+BETO-CI*(ALO-AL)
        F2V=2.0*VPU*(BO+B1*X)+B1*AL
        F2A=2.0*AL*(BO+B1*X)-B1*VPU+CI
        DAL=(F2/F2V-F1/F1V)/(F1A/F1V-F2A/F2V)
        DV=-F1/F1V-DAL*F1A/F1V
        AL=AL+DAL
        VPU=VPU+DV
        IF((ABS(DAL)+ABS(DV)).LT.TOL)GO TO 100
        KI(IP)=KI(IP)+1
        IF(KI(IP).LE.50)GO TO 50
        WRITE (6,70) IP,I,T,X,AL,VPU,HPU,F1,F2,DV,DAL
70      FORMAT(//2X,'FPUMP B.C TROUBLE'/2X,2I3,F9.3,8E14.4)
        STOP
100     LVB=1
        GO TO 50
150     IF(.NOT.(VPUO.GT.0.0.AND.VPU.LE.0.0))GO TO 200
        TVO=T
        IF(KODPHV.EQ.1.AND.DLY.LT.0.001)GO TO 220
200     IF(AL.GE.0.0.OR.KODNRR.EQ.0) THEN
        HND=H1
        VND=VPU*(VDI/VPUI)
        HNU=ZPU+HPU*HRAT
        VNU=VND
        BET=(AL*AL+VPU*VPU)*(BO+B1*X)
        GO TO 400
        ELSE
        KDNR=1
        GO TO 10
        ENDIF       
220     HND=CM
        VND=0.0
        HNU=ZPU
        VNU=0.0

        ELSE
        
        KI(IP) = 1        
300     HPU=FKNR*VPU*VPU
        H1=ZPU+HPU*HRAT-DHO*VPU*ABS(VPU)/VPUI**2
        F1=ZPU+HPU*HRAT-DHO*VPU*ABS(VPU)/VPUI**2-CM-B*VPU*(VDI/VPUI)
        F1V=2.0*FKNR*VPU*HRAT-B*(VDI/VPUI)-2.0*DHO*ABS(VPU)/VPUI**2
        DV=-F1/F1V
        VPU=VPU+DV
        IF(ABS(DV).LT.TOL1)GO TO 350
        KI(IP)=KI(IP)+1
        IF(KI(IP).LE.50)GO TO 300
        WRITE (6,320) IP,T,VPU,HPU,F1,DV
320     FORMAT(//2X,'F. PUMP NRR B.C TROUBLE'/2X,I3,F9.3,4E14.4)
        STOP
350     HND=H1
        VND=VPU*(VDI/VPUI)
        HNU=ZPU+HPU*HRAT
        VNU=VND
        AL=0.0
        GO TO 400

        ENDIF

400     CONTINUE
        RETURN
        END



	SUBROUTINE RPUMP (IP,T,ZPU,HRAT,VDI,VPUI,AL,VPU,WH,DHO,CM,B,
     *  HNU,HND,VNU,VND,KI,HPU)
	DIMENSION WH(10,89),KI(20)
	DATA PI,DX,TOL/3.1416,0.0714,0.0001/
	LVB=0
        KI(IP)=1
50      X=PI+ATAN2(VPU,AL)
        I=X/DX+1
        A1=(WH(IP,I+1)-WH(IP,I))/DX
        AO=WH(IP,I+1)-A1*I*DX
        HPU=(AL*AL+VPU*VPU)*(AO+A1*X)
        H1=ZPU+HPU*HRAT-DHO*VPU*ABS(VPU)/VPUI**2
        IF(LVB.EQ.1)GO TO 200
	F1=ZPU+HPU*HRAT-DHO*VPU*ABS(VPU)/VPUI**2-CM-B*VPU*(VDI/VPUI)
	F1V=2.0*VPU*(AO+A1*X)*HRAT+A1*AL*HRAT-B*(VDI/VPUI)-
     *  2.0*DHO*ABS(VPU)/VPUI**2
	DV=-F1/F1V
	VPU=VPU+DV
	IF (ABS(DV).LT.TOL) GO TO 100
        KI(IP)=KI(IP)+1
        IF(KI(IP).LE.50)GO TO 50
        WRITE (6,70) IP,I,T,X,VPU,HPU,F1,DV
70      FORMAT(//2X,'RPUMP B.C TROUBLE'/2X,2I3,F9.3,5E14.4)
	STOP
100     LVB=1
        GO TO 50
200     HND=H1
        VND=VPU*(VDI/VPUI)
        HNU=ZPU+HPU*HRAT
        VNU=VND

250	CONTINUE
	RETURN
	END


	SUBROUTINE FBST(KODBSV,IB,T,ALO,VPUO,BETO,CI,HRAT,
     *   VDI,VUI,VPUI,AL,VPU,BET,WH,WB,DHO,CM,CP,BR,BL,DLY,
     *   TVO,HNU,HND,VNU,VND,KI,HPU,KDNR,FKNR,CODNRR)
        CHARACTER * 10 CODNRR
	DIMENSION WH(5,89),WB(5,89),KI(20)
	DATA PI,DX,TOL,TOL1/3.1416,0.0714,0.0002,0.0001/
	IF(CODNRR.EQ.'YES')THEN
	KODNRR=1
	ELSE
	KODNRR=0
	ENDIF

10      IF(KDNR.EQ.0.)THEN 	

        LVB=0
        KI(IB)=1
50      X=PI+ATAN2(VPU,AL)
        I=X/DX+1
        A1=(WH(IB,I+1)-WH(IB,I))/DX
        AO=WH(IB,I+1)-A1*I*DX
        B1=(WB(IB,I+1)-WB(IB,I))/DX
        BO=WB(IB,I+1)-B1*I*DX
        HPU=(AL*AL+VPU*VPU)*(AO+A1*X)
        IF(LVB.EQ.1)GO TO 150
        F1=HPU*HRAT-DHO*VPU*ABS(VPU)/VPUI**2-CM-BR*VPU*(VDI/VPUI)+
     *  CP-BL*VPU*(VUI/VPUI)	 
        F1V=2.0*VPU*(AO+A1*X)*HRAT+A1*AL*HRAT-BR*(VDI/VPUI)-BL*
     *  (VUI/VPUI)-2.0*DHO*ABS(VPU)/VPUI**2
        F1A=2.0*AL*(AO+A1*X)*HRAT-A1*VPU*HRAT
        F2=(AL*AL+VPU*VPU)*(BO+B1*X)+BETO-CI*(ALO-AL)
        F2V=2.0*VPU*(BO+B1*X)+B1*AL
        F2A=2.0*AL*(BO+B1*X)-B1*VPU+CI
        DAL=(F2/F2V-F1/F1V)/(F1A/F1V-F2A/F2V)
        DV=-F1/F1V-DAL*F1A/F1V
        AL=AL+DAL
        VPU=VPU+DV
        IF((ABS(DAL)+ABS(DV)).LT.TOL)GO TO 100
        KI(IB)=KI(IB)+1
        IF(KI(IB).LE.50)GO TO 50
        WRITE (6,70) IB,I,T,X,AL,VPU,HPU,F1,F2,DV,DAL
70      FORMAT(//2X,'FBST B.C TROUBLE'/2X,2I3,F9.3,8E14.4)
        STOP
100     LVB=1
        GO TO 50
150     IF(.NOT.(VPUO.GT.0.0.AND.VPU.LE.0.0))GO TO 200
        TVO=T
        IF(KODBSV.EQ.1.AND.DLY.LT.0.001)GO TO 220
200     IF(AL.GE.0.0.OR.KODNRR.EQ.0) THEN
	VNU=VPU*(VUI/VPUI)
	VND=VPU*(VDI/VPUI)
	HNU=CP-BL*VNU
	HND=CM+BR*VND
        BET=(AL*AL+VPU*VPU)*(BO+B1*X)
        GO TO 400
        ELSE
        KDNR=1
        GO TO 10
        ENDIF       
220     HND=CM
        VND=0.0
        HNU=CP
        VNU=0.0

        ELSE
        
        KI(IB) = 1        
300     HPU=FKNR*VPU*VPU
        F1=HPU*HRAT-DHO*VPU*ABS(VPU)/VPUI**2-CM-BR*VPU*(VDI/VPUI)+
     *  CP-BL*VPU*(VUI/VPUI)
        F1V=2.0*FKNR*VPU*HRAT-BR*(VDI/VPUI)-BL*(VUI/VPUI)-
     *  2.0*DHO*ABS(VPU)/VPUI**2
        DV=-F1/F1V
        VPU=VPU+DV
        IF(ABS(DV).LT.TOL1)GO TO 350
        KI(IB)=KI(IB)+1
        IF(KI(IB).LE.50)GO TO 300
        WRITE (6,320) IB,T,VPU,HPU,F1,DV
320     FORMAT(//2X,'FBST NRR B.C TROUBLE'/2X,I3,F9.3,4E14.4)
        STOP
350     VNU=VPU*(VUI/VPUI)
        VND=VPU*(VDI/VPUI)
        HNU=CP-BL*VNU
	HND=CM+BR*VND
        AL=0.0
        GO TO 400

        ENDIF

400     CONTINUE
        RETURN
        END



	SUBROUTINE RBST(IB,T,HRAT,VUI,VDI,VPUI,AL,VPU,WH,DHO,CM,CP,
     *  BR,BL,HNU,HND,VNU,VND,KI,HPU)
	DIMENSION WH(5,89),KI(20)
	DATA PI,DX,TOL/3.1416,0.0714,0.0001/
	LVB=0
        KI(IB)=1
50      X=PI+ATAN2(VPU,AL)
        I=X/DX+1
        A1=(WH(IB,I+1)-WH(IB,I))/DX
        AO=WH(IB,I+1)-A1*I*DX
        HPU=(AL*AL+VPU*VPU)*(AO+A1*X)
        IF(LVB.EQ.1)GO TO 200
	F1=HPU*HRAT-DHO*VPU*ABS(VPU)/VPUI**2-CM-BR*VPU*(VDI/VPUI)+
     *  CP-BL*VPU*(VUI/VPUI)
	F1V=2.0*VPU*(AO+A1*X)*HRAT+A1*AL*HRAT-BR*(VDI/VPUI)-
     *  BL*(VUI/VPUI)-2.0*DHO*ABS(VPU)/VPUI**2
	DV=-F1/F1V
	VPU=VPU+DV
	IF (ABS(DV).LT.TOL) GO TO 100
        KI(IB)=KI(IB)+1
        IF(KI(IB).LE.50)GO TO 50
        WRITE (6,70) IB,I,T,X,VPU,HPU,F1,DV
70      FORMAT(//2X,'RBST B.C TROUBLE'/2X,2I3,F9.3,5E14.4)
	STOP
100     LVB=1
        GO TO 50
200     VNU=VPU*(VUI/VPUI)
        VND=VPU*(VDI/VPUI)
	HNU=CP-BL*VNU
	HND=CM+BR*VND	

250	CONTINUE
	RETURN
	END



	SUBROUTINE SVALVE(BL,ARL,CP,CM,KSV,OSV,ZSV,SVC,VSV,
     *       HNU,HND,VNU,VND,AIRC)
	BR=BL
	ARR=ARL
	IF(KSV.EQ.0)THEN
	  VSV=0.0
	  HNU=(CM*BL+CP*BR*ARL/ARR)/(BL+BR*ARL/ARR)
	  HND=HNU
	  VNU=(CP-HNU)/BL
	  VND=(HND-CM)/BR
	ELSE
	  CSV1=ARL/BL+ARR/BR
	  CSV2=(ARL/BL)*CP+(ARR/BR)*CM
	  AA=(1.0/(OSV*SVC))**2
	  BB=1.0/CSV1
	  CC=ZSV-CSV2/CSV1
	  IF(CC.GT.0.0)THEN
	  AA=AA/AIRC**2
	  VSV=(BB-SQRT(BB*BB+4.0*AA*CC))/(2.0*AA)
	  ELSE
	  VSV=(-BB+SQRT(BB*BB-4.0*AA*CC))/(2.0*AA)
	  END IF
	  HNU=ZSV+VSV*ABS(VSV)*AA
	  HND=HNU
          VNU=(CP-HNU)/BL
	  VND=(HND-CM)/BR
	END IF
	RETURN
	END
